Remove leading zeros via parameter expansion
# cf. http://codesnippets.joyent.com/posts/show/1816 i=004555 printf "%s\n" "${i}" printf "%s\n" "${i#"${i%%[!0]*}"}" i="${i#"${i%%[!0]*}"}" printf "%s\n" "${i}"
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!)
# cf. http://codesnippets.joyent.com/posts/show/1816 i=004555 printf "%s\n" "${i}" printf "%s\n" "${i#"${i%%[!0]*}"}" i="${i#"${i%%[!0]*}"}" printf "%s\n" "${i}"
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" cp -ip "${dockplistfile}" "${dockplistfile}.orig" plutil -convert xml1 -o /dev/fd/1 "${dockplistfile}" | /usr/bin/pl plutil -convert xml1 -o ~/Desktop/com.apple.dock.plist "${dockplistfile}" open -e ~/Desktop/com.apple.dock.plist # get an example entry from $dockplistfile (last application in the Dock) # cf. http://codesnippets.joyent.com/posts/show/1814 num_apps_dock=$(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l) echo ${num_apps_dock} /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num_apps_dock}'" "${dockplistfile}" #/usr/libexec/PlistBuddy -c "Print :persistent-apps:${num_apps_dock}" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num_apps_dock}':tile-data:file-data:_CFURLString" "${dockplistfile}" open /bin/bash # we will first create an example entry step by step # note: persistent-apps is an array that stores dictionaries dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" # get a free array index number (that is, the last + 1 array index) free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l) )) echo ${free_num_apps_dock} # add a dictionary to the persistent-apps array at array index ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}' dict" "${dockplistfile}" # print the dictionay in the persistent-apps array at array index ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" # add a dictionary named "tile-data" to the previously added dictionary /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data dict" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" # add the dictionary 'file-data' to the dictionary 'tile-data' /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data dict" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" # add key - value pairs to the file-data dictionary /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:thisIsAKey string 'this is a string value'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '/path/to/app'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" # delete the dictionay in the persistent-apps array at array index ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" # print all dictionaries of the persistent-apps array /usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" # short version dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l) )) echo ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:thisIsAKey string 'this is a string value'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '/path/to/app'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" :<<-'COMMENT' # example entry Dict { tile-data = Dict { file-data = Dict { thisIsAKey = this is a string value _CFURLString = /path/to/app _CFURLStringType = 0 } } } COMMENT #----------------------------------------------------------------------------- # app2dock # allows case insensitive name of application as input # uses lsregister & egrep -i # lsregister /usr/bin/sudo /bin/ln -is $(/usr/bin/locate *lsregister | /usr/bin/head -n 1) /bin/lsregister function app2dock() { declare appname dockplistfile free_num_apps_dock path dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" for appname in "${@}"; do appname="${appname%/}" path="$(/bin/lsregister -dump | /usr/bin/egrep -i -m 1 "path: +.*\/[^\/]*${appname}[^\/]*\.app$" | \ /usr/bin/sed -E 's/^[[:space:]]+path:[[:space:]]+//')" if [[ -z "${path}" ]]; then echo "No such application: ${appname}"; continue; fi path="${path%/}" if [[ ! -d "${path}" ]] || [[ "${path}" == "${path%.app}" ]]; then echo "Argument error: ${path}"; continue; fi free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | \ /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | /usr/bin/wc -l) )) #echo ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '${path}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" #/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null done /usr/bin/killall -HUP Dock return 0 } # app2dock_fp # only allows full file path to application as input function app2dock_fp() { declare dockplistfile free_num_apps_dock path dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" for path in "${@}"; do path="${path%/}" if [[ ! -d "${path}" ]] || [[ "${path}" == "${path%.app}" ]]; then echo "Argument error: ${path}"; continue; fi free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | \ /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | /usr/bin/wc -l) )) #echo ${free_num_apps_dock} /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '${path}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" #/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null done /usr/bin/killall -HUP Dock return 0 } # remove_app_from_dock # allows case insensitive name of application as input function remove_app_from_dock() { declare appname dockplistfile nums="" dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist" for appname in "${@}"; do appname="${appname%/}" nums="" nums=$(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | \ /usr/bin/nl | /usr/bin/egrep -i "${appname}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn) if [[ -z "${nums}" ]]; then echo "No application: ${appname} found in the Dock."; continue; fi for num in ${nums}; do /usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${num}'" "${dockplistfile}" done # test #for num in ${nums}; do # /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num}'" "${dockplistfile}" #done /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null done /usr/bin/killall -HUP Dock return 0 } app2dock_fp /Applications/Chess.app /Applications/Chess.app app2dock chess grapher remove_app_from_dock chess grapher
# version 1 function add2path() { declare defaultpath path pathvar # add the new path at the beginning of $PATHVARIABLE if [[ "${1}" == '-b' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi path="${3}:${2}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' # add the new path at the end of $PATHVARIABLE elif [[ "${1}" == '-e' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary pathvar="$(printf "%s" "${2//:/$'\n'}" | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')" path="${pathvar}:${3}" path="${path//::/:}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' # default: add the new path at the beginning of $PATHVARIABLE elif [[ $# -eq 2 ]]; then if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi path="${2}:${1}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' else defaultpath=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' defaultpath="${defaultpath// /}" # remove spaces defaultpath="${defaultpath//$'\n'/:}" # convert newlines into colons defaultpath="${defaultpath#:}" # cut off leading colon defaultpath="${defaultpath%:}" # cut off trailing colon printf "%s" "${defaultpath}" return 1 fi return 0 } # usage: # add2path [$PATHVARIABLE] /path/to/dir # add2path [-b|-e] [$PATHVARIABLE] /path/to/dir # store the original $PATH OPATH="${PATH}" # create test directories testdir="${HOME}/Desktop/testdir" /bin/mkdir -p "${testdir}" for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; } # list test directories echo "${testdir}" for ((i=0;i<=5;i++)) { echo "${testdir}${i}"; } add2path "${PATH}" "${testdir}" | tr ':' '\n' | nl printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(add2path "${PATH}" "${testdir}")" printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}")"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl add2path | tr ':' '\n' | nl add2path abc | tr ':' '\n' | nl add2path -b "${PATH}" "${testdir}" | tr ':' '\n' | nl add2path -e "${PATH}" "${testdir}" | tr ':' '\n' | nl # restore original $PATH export PATH="${OPATH}" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #-------------------------------------------------------------- # version 2 function add2path() { # add the new path at the beginning of $PATHVARIABLE if [[ "${1}" == '-b' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi printf "%s" "${3}:${2}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' # add the new path at the end of $PATHVARIABLE elif [[ "${1}" == '-e' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary pathvar="$(printf "%s" "${2}" | /usr/bin/tr ':' '\n' | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')" printf "%s" "${pathvar}:${3}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' # default: add the new path at the beginning of $PATHVARIABLE elif [[ $# -eq 2 ]]; then if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi printf "%s" "${2}:${1}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' else path=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E '/^:|:$/s/^:|:$//g' return 1 fi return 0 } # usage: # add2path [$PATHVARIABLE] /path/to/dir # add2path [-b|-e] [$PATHVARIABLE] /path/to/dir OPATH="${PATH}" testdir="${HOME}/Desktop/testdir" /bin/mkdir -p "${testdir}" for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(add2path "${PATH}" "${testdir}" )" printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="${OPATH}" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #--------------------------------------------------------------- function remove_from_path() { declare defaultpath newpath if [[ $# -ne 2 ]]; then defaultpath=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' defaultpath="${defaultpath// /}" # remove spaces defaultpath="${defaultpath//$'\n'/:}" # convert newlines into colons defaultpath="${defaultpath#:}" # cut off leading colon defaultpath="${defaultpath%:}" # cut off trailing colon printf "%s" "${defaultpath}" return 1 fi if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi newpath="$(printf "%s" "${1//:/$'\n'}" | /usr/bin/egrep -v "^${2}$" | /usr/bin/tr '\n' ':')" #newpath="${newpath// /}" # remove spaces newpath="${newpath#:}" # cut off leading colon newpath="${newpath%:}" # cut off trailing colon #printf "%s" "${newpath//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' printf "%s" "${newpath}" return 0 } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(remove_from_path "${PATH}" "/opt/local/bin" )" export PATH="$(remove_from_path "${PATH}" "/opt/local/xyz" )" export PATH="$(remove_from_path "${PATH}" "" )" export PATH="$(remove_from_path "${PATH}" )" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #--------------------------------------------------------------- man bash 2>/dev/null | less -p "Functions are executed" # 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).
<xsl:output method="html" />
find . -type d -name '.svn' -exec rm -rf {} \;
sudo rm /usr/local/mysql
sudo rm -rf /Library/StartupItems/MySQLCOM/