sudo shell
# cf. http://nevali.net/2006/04/sudo-and-su/ #function sudosh() { /usr/bin/sudo -H -i; return 0; } alias sudosh='/usr/bin/sudo -H -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://nevali.net/2006/04/sudo-and-su/ #function sudosh() { /usr/bin/sudo -H -i; return 0; } alias sudosh='/usr/bin/sudo -H -i'
# LAUNCHD ITEM /usr/bin/sudo /bin/bash -c ' yourname=$(/usr/bin/logname) LaunchdPlistFile="/Library/LaunchDaemons/user.${yourname}.launchd.test.plist" EX_MARK='!' /bin/cat > "${LaunchdPlistFile}" <<-EOF <?xml version="1.0" encoding="UTF-8"?> <${EX_MARK}DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <true/> <key>Debug</key> <true/> <key>UserName</key> <string>${yourname}</string> <key>GroupName</key> <string>${yourname}</string> <key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin</string> <key>YetAnotherEnvironmentVar</key> <string>/path/to/dir</string> </dict> <key>Label</key> <string>user.${yourname}.launchd.test</string> <key>ProgramArguments</key> <array> <string>/Users/${yourname}/Desktop/launchd_test.sh</string> </array> <key>RunAtLoad</key> <true/> <key>ServiceDescription</key> <string>Test ownership of launchd processes</string> <key>StandardErrorPath</key> <string>/Users/${yourname}/Desktop/launchd_test_error.log</string> <key>StandardOutPath</key> <string>/Users/${yourname}/Desktop/launchd_test.log</string> <key>StartInterval</key> <integer>20</integer> <key>Umask</key> <integer>002</integer> </dict> </plist> EOF ' : <<-'COMMENT' # switch UserName & GroupName <key>UserName</key> <string>${yourname}</string> <key>GroupName</key> <string>${yourname}</string> or <key>UserName</key> <string>root</string> <key>GroupName</key> <string>wheel</string> or ... COMMENT yourname=$(/usr/bin/logname) LaunchdPlistFile="/Library/LaunchDaemons/user.${yourname}.launchd.test.plist" open -e "${LaunchdPlistFile}" # SCRIPT LaunchdTestScript="/Users/${yourname}/Desktop/launchd_test.sh" /bin/cat > "${LaunchdTestScript}" <<-'EOF' #!/bin/bash # /bin/sh # cf. man 5 launchd.plist | less -p 'A daemon or agent launched by launchd SHOULD:' trap '/usr/bin/logger -i "received SIGTERM for launchd test"; exit 1' TERM # write stdout & stderr to console.log in /Library/Logs/Console/ # open -a Console # (requires /bin/bash to work correctly!) #exec >/dev/console 2>&1 echo echo "SYSTEM LOGS:" /usr/bin/logger -i "logger: $0" /usr/bin/syslog -s -l 1 "syslog: $0" echo echo echo 'id:' echo /usr/bin/id | /usr/bin/sed -E \ -e $'s/,? /\\\n/g' \ -e $'/groups=/s/groups=/groups:\\\n/' \ -e 's/=/ = /g' \ -e $'s/[()]/ /g' echo # cf. man mail | less -p HOME # cf. Read local Unix mail in Mail.app, http://codesnippets.joyent.com/posts/show/1392 #export HOME=/Users/$(/usr/bin/whoami) #echo "hello world $(date)" | /usr/bin/mail -s 'test mail' $(/usr/bin/whoami)@localhost echo echo "SHELL: $SHELL" echo "BASH: $BASH" echo echo "current set of shell flags: $-" # cf. help set echo echo "SHELLOPTS: $SHELLOPTS" echo echo 'set:' echo set echo printf "%q %q\n" "IFS:" "$IFS" echo echo 'env:' echo /usr/bin/env echo echo echo 'shopt ... on:' echo shopt | /usr/bin/egrep 'on$' #set -o | /usr/bin/egrep 'on$' echo cd ~ echo "HOME: $PWD" echo echo 'try to cd to /private/var/launchd/0' cd /private/var/launchd/0 return_code=$? echo "cd exit status: ${return_code}" echo if [[ ${return_code} -eq 0 ]]; then echo "ROOT PRIVILEGES"; else echo "NO ROOT PRIVILEGES"; fi echo echo "PPID: $PPID" echo echo "PID: $$" echo echo 'ps -p PID:' echo /bin/ps -p $PPID echo /bin/ps -p $$ echo echo 'lsof -p PID:' echo /usr/sbin/lsof -p $PPID 2>/dev/null echo /usr/sbin/lsof -p $$ 2>/dev/null echo echo "users: $(/usr/bin/users)" echo echo "groups: $(/usr/bin/groups)" # /usr/bin/id -Gn echo echo "logname: $(/usr/bin/logname)" echo echo "whoami: $(/usr/bin/whoami)" # /usr/bin/id -un echo; echo echo "#----------------------------------------------------------------------------------------------- $(date)" echo; echo exit 0 #exit 1 EOF open -e "${LaunchdPlistFile}" open -e "${LaunchdTestScript}" /usr/bin/sudo /usr/sbin/chown root:wheel "${LaunchdPlistFile}" /usr/bin/sudo /bin/chmod 0644 "${LaunchdPlistFile}" /usr/sbin/chown ${yourname}:${yourname} "${LaunchdTestScript}" /bin/chmod u+x "${LaunchdTestScript}" ls -l "${LaunchdPlistFile}" ls -l "${LaunchdTestScript}" # enable launchd item /usr/bin/sudo /bin/launchctl load -w "${LaunchdPlistFile}" 2>/dev/null # disable launchd item /usr/bin/sudo /bin/launchctl unload -w "${LaunchdPlistFile}" 2>/dev/null ls -l /Users/${yourname}/Desktop/launchd_test.log ls -l /Users/${yourname}/Desktop/launchd_test_error.log open -a Console /Users/${yourname}/Desktop/launchd_test.log #/usr/bin/srm -v /Users/${yourname}/Desktop/launchd_test.log open -a Console /Users/${yourname}/Desktop/launchd_test_error.log /usr/bin/sudo /bin/launchctl list /usr/bin/sudo /usr/bin/fs_usage | /usr/bin/egrep -i launchd_test # convert possible tabs to spaces #/usr/bin/expand "${LaunchdPlistFile}" > ~/Desktop/user.${yourname}.launchd.test.plist #open -e ~/Desktop/user.${yourname}.launchd.test.plist man 8 launchd man 5 launchd.plist man 5 launchd.conf man 1 launchctl #---------------------------------------------------------------- # set launchd log level to debug # cf. http://www.puredarwin.org/developers/booting/launchd # sudo launchctl log level debug sudo bash -c 'echo "log level debug" >> /private/etc/launchd.conf' echo 'log level debug' >> ${HOME}/.launchd.conf sudo nano /private/etc/launchd.conf ls -l /private/etc/launchd.conf # send all launchd logging output to /private/var/log/launchd.log # cf. http://developer.apple.com/technotes/tn2004/tn2124.html#SECLAUNCHDLOGGING sudo cp -ip /etc/syslog.conf /etc/syslog.conf-orig (cat /etc/syslog.conf-orig ; echo "launchd.* /var/log/launchd.log" ) | sudo cp /dev/stdin /etc/syslog.conf sudo kill -HUP `cat /var/run/syslog.pid` ls -1 /private/var/log/*log ls -l /private/var/log/launchd.log open -a Console /private/var/log/launchd.log
function iregex() { declare escaped_str iregexstr lower upper escaped_str="$(printf "%q" "${1}")" iregexstr= lower="$(printf "%s" "${escaped_str}" | /usr/bin/tr '[[:upper:]]' '[[:lower:]]')" upper="$(printf "%s" "${escaped_str}" | /usr/bin/tr '[[:lower:]]' '[[:upper:]]')" for ((i=0; i < "${#lower}"; i++)); do iregexstr="${iregexstr}[${lower:${i}:1}${upper:${i}:1}]" done printf "%s" "${iregexstr}" | /usr/bin/sed -E -e 's/\[([[:space:]])[[:space:]]\]/\1/g' -e 's/\[([[:punct:]])[[:punct:]]\]/\1/g' return 0 } iregex 'hello \ | / again!?' re="$(iregex 'hello \ | / again!?')" # replace the following string string='helLo \ | / again!?' re_str='hello \ | / again!?' iregex "${re_str}" regex="$(iregex "${re_str}")" # always use iregex "${re_str}" echo "${regex}" echo "${string}" | sed "s/${regex}/replacement worked/g" # / not escaped in regex echo "${string}" | sed "s|${regex}|replacement worked|g" # always use "s|${re}|replacement|" # note: printf does not escape / to \/ in function iregex above #printf "%q\n" "/|\ " printf "%q\n" '/|\' # cf. gnused, http://codesnippets.joyent.com/posts/show/1835 string='helLo \ | / again!?' echo "${string}" | gnused 's/hello \\ | \/ again!?/replacement worked/ig' echo "${string}" | gnused 's|hello \\ \| / again!?|replacement worked|ig' #--------------------------------------------------------- # replace the following string containing a tab character \t string=$'helLo \ | / \tagain!?' echo "${string}" iregex "${string}" re_str1='hello \ | / ' re_str2=$'\t' re_str3='again!?' re1="$(iregex "${re_str1}")" re2="${re_str2}" re3="$(iregex "${re_str3}")" echo "${string}" | sed "s|${re1}${re2}${re3}|replacement worked|g" echo "${string}" | gnused 's|hello \\ \| / \tagain!?|replacement worked|ig' #--------------------------------------------------------- TAB=$'\t' echo $'abc\tdef' echo $'abc\tdef' | sed $'s/\t//' echo $'abc\tdef' | sed s/$'\t'// echo $'abc\tdef' | sed "s/${TAB}//" echo $'abc\tdef' | sed $'s/\t/TAB/' echo $'abc\tdef' | sed $'s/\t/TAB/' | sed $'s/TAB/\t/' echo $'abc\tdef' | sed $'s/\t/TAB/' | sed s/TAB/$'\t'/ echo $'abc\tdef' | sed $'s/\t/TAB/' | sed "s/TAB/${TAB}/" # gnused echo $'abc\tdef' | sed 's/\t/TAB/' echo $'abc\tdef' | gnused 's/\t/TAB/' echo $'abc\tdef' | gnused $'s/\t/TAB/' echo $'abc\tdef' | gnused "s/${TAB}/TAB/" echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused $'s/TAB/\t/' echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused s/TAB/$'\t'/ echo $'abc\tdef' | gnused $'s/\t/TAB/' | gnused "s/TAB/${TAB}/" echo $'abc\tdef' | gnused 's/\t/TAB/' | gnused 's/TAB/\t/'
# 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
# usage: bds [-p num] [-t tag] [-u user] tag bds vim bds -p 1280 bds -u jvs bds -t plistbuddy bds -t tar bds -t ipfw -u jvs
#!/opt/local/bin/bash # "batch download snippets" from http://codesnippets.joyent.com and # convert them to text files using man textutil (available on Mac OS X 10.4 or later). # # Note: Old snippet versions will be automatically replaced by the downloaded snippets without a backup! # An alternative to man textutil is html2text, http://www.mbayer.de/html2text/ (which is available via MacPorts). # # Author: jv # License: The MIT License, http://www.opensource.org/licenses/mit-license.php # Copyright (c) 2008 jv # # cat /usr/local/bin/bds # # usage: bds [-p num] [-t tag] [-u user] tag declare BaseURL='http://codesnippets.joyent.com' declare download_dir="${HOME}/Desktop/Snippets" # make sure there is no trailing slash BaseURL="${BaseURL%/}" download_dir="${download_dir%/}" declare BasePostURL="${BaseURL}/posts/show" declare BaseTagURL="${BaseURL}/tag" declare BaseUserURL="${BaseURL}/user" # make sure there is no trailing slash BasePostURL="${BasePostURL%/}" BaseTagURL="${BaseTagURL%/}" BaseUserURL="${BaseUserURL%/}" # man textutil declare InputEncoding='utf-8' declare OutputEncoding='utf-8' export IFS=$' \t\n' # function to download a single post specified by a post number: bds -p num # cf. snippet, http://codesnippets.joyent.com/posts/show/1282 function snippet() { declare NL OPWD file outputfile postnum title url if [[ "${1//[[:digit:]]/}" != "" ]]; then echo "Argument error. No positive integer: ${1}"; return 1; fi postnum="${1}" url="${BasePostURL}/${postnum}" download_dir="${download_dir}/single-downloads" /bin/mkdir -p "${download_dir}" OPWD="${PWD}" cd "${download_dir}" /usr/bin/curl -L -O -s --max-time 25 "${url}" || exit 1 # download snippet web page file="${download_dir}/${url##*/}" trap '/bin/rm -f "${file}"; exit 0' 0 1 2 13 15 # get title of downloaded web page #title="$(/usr/bin/sed -E -n -e '/<[tT][iI][tT][lL][eE]>/{s/^.*<[tT][iI][tT][lL][eE]>(.*)<\/[tT][iI][tT][lL][eE]>.*$/\1/p;q;}' "${file}" | \ # /usr/bin/sed -E -e 's/\[[^][:space:]]*\]//g')" # delete [xxx] tag elements of title title="$(/usr/bin/egrep -m 1 -io '<title>.*</title>' "${file}" | /usr/bin/sed -E -e 's/^<title>[[:space:]]*|[[:space:]]*<\/title>$//g' \ -e 's/\[[^][:space:]]*\]//g')" # delete [xxx] tag elements of title title="${title//CodeSnippets:/}" title="${title//\//:}" title="${title// /_}" title="${title//[[:cntrl:]]/}" title="${title%"${title##*[!_]}"}" # remove trailing underscores if [[ $title == '_CodeDrive_Snippets_courtesy_of_Peter_Coopers_handy_little_app' ]] || [[ -z "$title" ]]; then printf "\e[0K\e[31m%s\e[0m: %s\n" "couldn't access" "${url}" /bin/rm "${file}" return 1 fi outputfile="${download_dir}/${postnum}_${title}.txt" #outputfile="${download_dir}/${title}.txt" # without post number prefix #outputfile="${outputfile//__/_}" # uniq underscores printf "\n\e[0K\e[1;30m%s\e[0m: %s\n\n" "saved as" "${outputfile}" /usr/bin/textutil -output "${outputfile}" -convert txt -inputencoding "${InputEncoding}" -encoding "${OutputEncoding}" "${file}" /bin/rm "${file}" # escape backslashes # man bash 2>/dev/null | less -p 'Each command in a pipeline' #outputfile="$(printf "%q" "${outputfile}")" # cf. help printf outputfile="${outputfile//\\/\\\\}" NL=$'\\\n' cat <<EOF | /bin/ed -s "${outputfile}" H ,g/Snippets is a public source code repository/1,/Snippets is a public source code repository/d ,g/You need to create an account or log in to post comments to this site//You need to create an account or log in to post comments to this site/,\$d ,g|(See related posts)$|s|.See related posts.|${NL}${NL}| ,g|^to.* by.* on .*[[:digit:]]$|s|^to\(.*\) by\(.*\) on \(.*[[:digit:]]\)$|${NL}${NL}Author:\2${NL}Date: \3${NL}URL: ${url}${NL}Tags:\1${NL}| ,g|^Comments on this post$|s|\(Comments on this post\)|${NL}\1:| ,g| posts on .* at |s|\(.* posts on .* at .*\)|${NL}\1:| w EOF # additional ed commands # delete line numbers # ,g|^[[:space:]]*[[:digit:]]\{1,\}[[:space:]]\{1,3\}|s|^[[:space:]]*[[:digit:]]\{1,\}[[:space:]]\{1,3\}\(.*\)$|\1| # delete range of lines # 4,11d cd "${OPWD}" return 0 } #----------------------------------------- end of function snippet declare pflag tflag uflag declare cnt count dir_name file no_posts_check NL OPWD outputfile postnum tagsite title url urls website if [[ $# -eq 0 ]]; then printf "%s\n%s\n" 'No arguments given!' "Usage: ${0##*/} [-p num] [-t tag] [-u user] tag" 1>&2 exit 1 fi while getopts ":p:t:u:" option do case $option in p) pflag="$OPTARG" ;; t) tflag="$OPTARG" ;; u) uflag="$OPTARG" ;; [?]) printf "%s\n%s\n" 'Argument error!' "Usage: ${0##*/} [-p num] [-t tag] [-u user] tag" 1>&2; exit 1;; *) ;; esac done shift $(($OPTIND - 1)) if [[ $# -eq 1 ]]; then dir_name="${1}" tagsite="${BaseTagURL}/${1}" elif [[ $# -gt 1 ]]; then printf "%s\n%s\n" 'Too many arguments!' "Usage: ${0##*/} [-p num] [-t tag] [-u user] tag" 1>&2 exit 1 elif [[ -n "${pflag}" ]]; then snippet "${pflag}" exit 0 elif [[ -n "${tflag}" ]] && [[ -n "${uflag}" ]]; then dir_name="${tflag}-${uflag}" tagsite="${BaseUserURL}/${uflag}/tag/${tflag}" elif [[ -n "${tflag}" ]]; then dir_name="${tflag}" tagsite="${BaseTagURL}/${tflag}" elif [[ -n "${uflag}" ]]; then dir_name="${uflag}" tagsite="${BaseUserURL}/${uflag}" else printf "%s\n%s\n" 'Argument error!' "Usage: ${0##*/} [-p num] [-t tag] [-u user] tag" 1>&2 exit 1 fi tagsite="${tagsite%/}" #echo $dir_name #echo $tagsite count=1 cnt=0 curl_max_time=20 website='' no_posts_check='' NL=$'\\\n' download_dir="${download_dir}/${dir_name//\//:}" download_dir="${download_dir%/}" /bin/mkdir -p "${download_dir}" OPWD="${PWD}" cd "${download_dir}" # print download directory printf "\n\e[0K\e[1;30m%s\e[0m: %s\n\n" "download directory" "${download_dir}" while [[ -z "${no_posts_check}" ]]; do # download website of the form: # http://somewebsite.com/tag/bash/1, # http://somewebsite.com/user/name/1 or # http://somewebsite.com/user/name/tag/bash/1 website="$(/usr/bin/curl -L -s --max-time $curl_max_time "${tagsite}/${count}" )" if [[ $? -ne 0 ]]; then printf "\e[0K\e[31m%s\e[0m: %s\n" "curl_max_time ${curl_max_time}" "${tagsite}/${count}" exit 1 fi #if [[ -n "$(printf "%s" "${website}" | /usr/bin/egrep -o 'Application error \(Apache\)')" ]]; then #no_posts_check='Application error (Apache)' #printf "\e[0K\e[31m%s\e[0m: %s\n" "no further posts" "${no_posts_check}" #fi if [[ -n "$(printf "%s" "${website}" | /usr/bin/egrep -o '>No posts<')" ]]; then no_posts_check='>No posts<' #printf "\e[0K\e[31m%s\e[0m: %s\n" "no further posts" "${no_posts_check}" fi : <<-'COMMENT' # works for Bash 3.0 or later if [[ "${website}" =~ '>No posts<' ]]; then no_posts_check="${BASH_REMATCH[0]}" #printf "\e[0K\e[31m%s\e[0m: %s\n" "no further posts" "${no_posts_check}" fi COMMENT if [[ -z "${no_posts_check}" ]]; then # extract relevant post URLs #urls=( $(printf "%s\n" "${website}" | /usr/bin/sed -E -n -e "s|^.* href=\"(/posts/show/[[:digit:]]+)\".*$|${BaseURL}\1|p;g") ) urls=( $(printf "%s\n" "${website}" | /usr/bin/egrep -o 'href="/posts/show/[[:digit:]]+"' | /usr/bin/sed -E -n -e "s|href=\"(/posts/show/[[:digit:]]+)\"|${BaseURL}\1|p;g") ) for ((i=0; i < "${#urls[@]}"; i++)); do url="${urls[${i}]}" postnum="${url##*/}" file="${download_dir}/${postnum}" trap '/bin/rm -f "${file}"; exit 0' 0 1 2 13 15 /usr/bin/curl -L -O -s --max-time $curl_max_time "${url}" if [[ $? -ne 0 ]]; then printf "\e[0K\e[31m%s\e[0m: %s\n" "curl_max_time ${curl_max_time}" "${url}" continue fi # get title of downloaded web page #title="$(/usr/bin/sed -E -n -e '/<[tT][iI][tT][lL][eE]>/{s/^.*<[tT][iI][tT][lL][eE]>(.*)<\/[tT][iI][tT][lL][eE]>.*$/\1/p;q;}' "${file}" | \ # /usr/bin/sed -E -e 's/\[[^][:space:]]*\]//g')" # delete [xxx] tag elements of title title="$(/usr/bin/egrep -m 1 -io '<title>.*</title>' "${file}" | /usr/bin/sed -E -e 's/^<title>[[:space:]]*|[[:space:]]*<\/title>$//g' \ -e 's/\[[^][:space:]]*\]//g')" # delete [xxx] tag elements of title title="${title//CodeSnippets:/}" title="${title//\//:}" title="${title// /_}" title="${title//[[:cntrl:]]/}" title="${title%"${title##*[!_]}"}" # remove trailing underscores #printf "%s\n" "${title}" if [[ $title == '_CodeDrive_Snippets_courtesy_of_Peter_Coopers_handy_little_app' ]] || [[ -z "$title" ]]; then printf "\e[0K\e[31m%s\e[0m: %s\n" "couldn't access" "${url}" /bin/rm "${file}" continue fi outputfile="${download_dir}/${postnum}_${title}.txt" #outputfile="${download_dir}/${title}.txt" # without post number prefix #outputfile="${outputfile//__/_}" # uniq underscores let cnt++ printf "\e[0K\e[1;32m%-6s\e[0m %s\n" "${cnt}" "${outputfile##*/}" /usr/bin/textutil -output "${outputfile}" -convert txt -inputencoding "${InputEncoding}" -encoding "${OutputEncoding}" "${file}" /bin/rm "${file}" # escape backslashes # man bash 2>/dev/null | less -p 'Each command in a pipeline' #outputfile="$(printf "%q" "${outputfile}")" # cf. help printf outputfile="${outputfile//\\/\\\\}" # edit $outputfile in-place with man ed # first delete lines at the beginning & end, # then remove the string 'See related posts' and add some newlines with $NL, # then convert the line 'to...by...on' to line 'Author:...', line 'Date:...', line 'URL:...' and line 'Tags:...' # and finally the last two ed commands insert two further newlines with $NL cat <<EOF | /bin/ed -s "${outputfile}" H ,g/Snippets is a public source code repository/1,/Snippets is a public source code repository/d ,g/You need to create an account or log in to post comments to this site//You need to create an account or log in to post comments to this site/,\$d ,g|(See related posts)$|s|.See related posts.|${NL}${NL}| ,g|^to.* by.* on .*[[:digit:]]$|s|^to\(.*\) by\(.*\) on \(.*[[:digit:]]\)$|${NL}${NL}Author:\2${NL}Date: \3${NL}URL: ${url}${NL}Tags:\1${NL}| ,g|^Comments on this post$|s|\(Comments on this post\)|${NL}\1:| ,g| posts on .* at |s|\(.* posts on .* at .*\)|${NL}\1:| w EOF # additional ed commands # delete line numbers # ,g|^[[:space:]]*[[:digit:]]\{1,\}[[:space:]]\{1,3\}|s|^[[:space:]]*[[:digit:]]\{1,\}[[:space:]]\{1,3\}\(.*\)$|\1| # delete range of lines # 4,11d done # for let count++ fi done # while cd "${OPWD}" exit 0
path=' /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /usr/bin /bin /usr/sbin /sbin ' path="$(printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E 's/^:|:$//g')" printf "%s\n" "$path" | tr ':' '\n' export PATH="${path}" cd ~/Desktop curl -L -O http://ftp.gnu.org/gnu/sed/sed-4.1.4.tar.gz cd ~/Desktop/sed-4.1.4 # cf. Compiling GNU coreutils on Mac OS X, # http://codesnippets.joyent.com/posts/show/1799 ./configure --prefix=/usr/local/gnucoreutils make /usr/bin/sudo /usr/bin/make install ls -l /usr/local/gnucoreutils/bin/sed /usr/local/gnucoreutils/bin/sed --version man /usr/local/gnucoreutils/man/man1/sed.1 /usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/sed /usr/local/bin/gnused # use GNU sed with case insensitive option echo Tool | sed 's/[Tt]/c/' echo Tool | gnused 's/t/c/i' # cf. "Extract title from HTML web page" on http://bosetsu.org/pub/docs/shell_cheatsheet.html curl -L -s http://codesnippets.joyent.com/posts/show/1835 | \ gnused -n 's/.*<[tT][iI][tT][lL][eE]>\(.*\)<\/[tT][iI][tT][lL][eE]>.*/\1/p;T;q' # cf. http://www.pixelbeat.org/cmdline.html curl -L -s http://codesnippets.joyent.com/posts/show/1835 | \ gnused -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'
man 2>/dev/null bash | less -p 'Quoting' man 2>/dev/null bash | less -p "Words of the form \\$'string'" man 2>/dev/null bash | less -p 'single quote' echo '\'' echo $'\'' echo $'\x27' echo $'\047' echo "'" echo 'str1'"'"'str2' # 'str1' + "'" + 'str2' echo $'single quote \' & double quote "' | sed $'s/& /\\\n/' str="str's" echo "${str//\'/''}"
#!/bin/sh ext="wav" for f in *.$ext do time afconvert -v -f m4af -d aac -b 262144 $f done
# 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}"
var="BASH"; echo "unix scripting" | awk '{gsub(/unix/,"'"${var}"'"); print}' var="BASH"; echo "unix scripting" | awk '{gsub(/unix/,"'"$(echo ${var})"'"); print}' var="BASH"; echo "unix scripting" | awk -v v="$var" '{sub(/unix/,v); print}'