Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Debugging a file name with a backslash character in Bash

# create a file name containing a backslash character \
file=${HOME}/Desktop/'te:st\file'.txt
echo "${file}"
echo "${file}" | sed -n -e 'l'

echo 'This is a test case for a file name containing a backslash \ character!' > "${file}"
open -e "${file}"

set -vx

# note: avoid trailing spaces in ed commands
cat <<EOF | /bin/ed -s "${file}"
H
,g|^This|s|test case|SUCCESSFUL TEST CASE|
w
EOF

open -e "${file}"


# escape backslashes
cat <<EOF | /bin/ed -s "${file//\\/\\\\}"
H
,g|^This|s|test case|SUCCESSFUL TEST CASE|
w
EOF

open -e "${file}"


# printf "%q"
help printf | sed -E "s/(%q)/$(printf '\e[1m\\1\e[m')/"
echo "${file}"
echo "$(printf "%q" "${file}")"   # cf. help printf
echo "$(printf "%q" "${file}")" | sed -n -e 'l'

# escape file name
cat <<EOF | /bin/ed -s "$(printf "%q" "${file}")"
H
,g|^This|s|backslash|BACKSLASH|
w
EOF

open -e "${file}"


echo "${file}"
echo "${file}" | sed -n -e 'l'

file="${file//\\/\\\\}"
echo "${file}"
echo "${file}" | sed -n -e 'l'


# references
man bash 2>/dev/null | less -p 'backslash'
man bash 2>/dev/null | less -p 'Each command in a pipeline'
man bash 2>/dev/null | less -p 'Functions are executed'
help printf | sed -E "s/(%q)/$(printf '\e[1m\\1\e[m')/"
open http://en.wikipedia.org/wiki/Filename


# "Each command in a pipeline is executed as a separate process (i.e., in a subshell)."
# From: man bash

# "Functions are executed in the context of the current shell; no new process is created to 
# interpret  them (contrast this with the execution of a shell script)."
# From: man bash

# "Unix-like systems are an exception, as the only control character forbidden in file names 
# is the null character, as that's the end-of-string indicator in C. Trivially, Unix also 
# excludes the path separator / from appearing in filenames."
# From: http://en.wikipedia.org/wiki/Filename

charpal - launch the Character Palette

/usr/bin/locate *CharPaletteServer.app

function charpal() {
   /usr/bin/open /System/Library/Components/CharacterPalette.component/Contents/SharedSupport/CharPaletteServer.app
   return 0
}


Handling control characters on the command line

# cf. http://en.wikipedia.org/wiki/Control_character and 
# http://ascii.cl/control-characters.htm

printf '\033'"\n" | cat -vet -
printf '\x0d'"\n" | cat -vet -

[ctrl-v][esc]
[ctrl-v][ctrl-m]

printf '[ctrl-v][ctrl-m]' | cat -vet -; echo


# cf. http://codesnippets.joyent.com/posts/show/1630
printf !:1 | sed -n -e 'l' 

# for an alternative to !:1 try [esc][ctrl][y]
printf !:1 | pbcopy    

# cf. man ascii
printf $(pbpaste) | od -A n -b
printf $(pbpaste) | od -A n -t xC
printf $(pbpaste) | od -A n -t dC

printf $(pbpaste) | od -A n -c
printf $(pbpaste) | od -A n -a

echo $'hel\rlo' 
echo $'hel\rlo' | sed s/[ctrl-v][ctrl-m]//
echo $'hel\rlo' | sed 's/[ctrl-v][ctrl-m]//'
echo $'hel\rlo' | sed "s/[ctrl-v][ctrl-m]//"
echo $'hel\rlo' | sed s/$'[ctrl-v][ctrl-m]'//


man infocmp
infocmp --help

infocmp -1 | grep '\^'
infocmp -1 | grep '\<k'
infocmp -L1 | grep '\^'
infocmp -I1 | grep '\^'
infocmp -C1 | grep '\^'
infocmp -L | grep cursor


tput cup 0 0
clear
tput cup 0 0 | cat -vet -; echo
tput cup 0 0 | sed -n -e 'l'
tput cup 0 0 | od -A n -c | sed '$,$d'
tput cup 0 0 | od -A n -a | sed '$,$d'
tput cup 0 0 | od -A n -b | sed '$,$d'
tput cup 0 0 | od -A n -t xC | sed '$,$d'

tput sgr0 | cat -vet -; echo
tput cuu | cat -vet -; echo
tput up | cat -vet -; echo
tput dl1 | cat -vet -; echo

tput kbs | wc -c
tput kbs | ruby -n -e 'p $_.to_s'


printf "%s\000\n" "str" | cat -vet -
printf "%s\000\n" "str" | od -A n -b | sed '$,$d'
printf "%s\000\n" "str" | sed -n -e 'l'
printf "%s\000\n" "str" | ruby -n -e 'p $_.to_s'

kv - launch Keyboard Viewer

locate -i *keyboardviewerserver

alias kv='/usr/bin/open /System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/KeyboardViewerServer.app'

kv


# cf. Accented letters and other symbols on the Mac,
# http://mac4translators.blogspot.com/2008/08/accented-letters-and-other-symbols-on.html

[cmd]     #  hold the command key
[alt]
[alt][shift]