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!)

About this user

jvscode [[at]] fastmail [[dot]] fm

Sorting arrays in Bash

export PATH="$(/usr/sbin/sysctl -n user.cs_path)"

export IFS=$'\n'
#export IFS=$' \t\n'

ar1=('g h c' abc def 123)
ar2=('1 2 3' ABC ghc DEF)
ar3=('-x' '! & ?' $'a test\nsentence' $'another test\nsentence\n.' a64bitapp a32bitapp)

printf "%s\n" "${#ar1[@]}" "${#ar2[@]}" "${#ar3[@]}"  


# ar3
for ((i=0; i < "${#ar3[@]}"; i++)); do echo "${ar3[$i]}"; done | sort | nl
for ((i=0; i < "${#ar3[@]}"; i++)); do echo ${ar3[$i]}; done | sort | nl
for ((i=0; i < "${#ar3[@]}"; i++)); do echo "${ar3[$i]}" | ruby -0777 -n -e 'p $_.to_s'; done | nl
for ((i=0; i < "${#ar3[@]}"; i++)); do printf -- "${ar3[$i]}\n" | ruby -0777 -n -e 'p $_.to_s'; done | nl


# sort a single array
ar3sorted=( $(for ((i=0; i < "${#ar3[@]}"; i++)); do echo ${ar3[$i]}; done | sort) )
for ((i=0; i < "${#ar3sorted[@]}"; i++)); do echo "${ar3sorted[$i]}" | ruby -0777 -n -e 'p $_.to_s'; done | nl
for ((i=0; i < "${#ar3sorted[@]}"; i++)); do printf "%s\n" "${ar3sorted[$i]}" | ruby -0777 -n -e 'p $_.to_s'; done | nl


# adding \000\n as array item delimiter
printf "%s\000\n" "${ar3[@]}"  | sed -n -e 'l'
printf "%s\000\n" "${ar3[@]}"  | ruby -n -e 'p $_.to_s'
printf "%s\000\n" "${ar3[@]}"  | ruby -0777 -n -e 'p $_.to_s'
printf "%s\000\n" "${ar3[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | ruby -n -e 'p $_.to_s'
printf "%s\000\n" "${ar3[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | sed -e 's/^NEWLINE//' | ruby -n -e 'p $_.to_s'


ar3sorted=( $(printf "%s\000\n" "${ar3[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | sed -e 's/^NEWLINE//' | sort) )
for ((i=0; i < "${#ar3sorted[@]}"; i++)); do printf -- "${ar3sorted[$i]//NEWLINE/\n}" | ruby -0777 -n -e 'p $_.to_s'; done | nl


# sort multiple arrays; convert embedded newline characters into single spaces
ar4=( $(printf "%s\n\000" "${ar1[@]}" "${ar2[@]}" "${ar3[@]}" | tr '\n' ' ' | tr '\000' '\n' | sort) )

# sort multiple arrays; preserve embedded newline characters as NEWLINE
ar4=( $(printf "%s\000\n" "${ar1[@]}" "${ar2[@]}" "${ar3[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | sed -e 's/^NEWLINE//' | sort) )


printf "%s\n" "${#ar4[@]}"  
printf "%s\n" "${ar4[@]}" | nl  

for ((i=0; i < "${#ar4[@]}"; i++)); do echo ${ar4[$i]//NEWLINE/\\n}; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do echo -e ${ar4[$i]//NEWLINE/\\n}; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do printf -- ${ar4[$i]//NEWLINE/\\n}"\n"; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do printf -- "${ar4[$i]//NEWLINE/\n}\n"; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do printf "%s\n" ${ar4[$i]//NEWLINE/\\n}; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do printf -- "${ar4[$i]//NEWLINE/\n}\n" | ruby -0777 -n -e 'p $_.to_s'; done | nl
for ((i=0; i < "${#ar4[@]}"; i++)); do printf "%s\n" "${ar4[$i]//NEWLINE/\n}" | ruby -0777 -n -e 'p $_.to_s'; done | nl

Switch to the login window from the command line

# cf. http://www.mactipper.com/2008/07/securely-lock-your-mac-system.html,
# http://www.macosxhints.com/article.php?story=20040724203315798 and
# http://www.apple.com/downloads/macosx/automator/lockdesktop.html

/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID username


alias lo='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

# <ctrl> + l
help bind
bind -P | grep -i c-l
bind -x '"\C-l"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

# <esc> + l
#bind -x '"\M-l"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'
bind -x '"\el"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'


#---------------------------------------------


open -a 'Keychain Access'

# go to  Keychain Access > Preferences ... > General > activate "Show Status in Menu Bar" 
# now you can use "Lock Screen" via the Keychain Access menu bar item

Password request dialog from the command line with AppleScript

unset -v input

#input="$(/usr/bin/osascript <<-'__HEREDOC__'
input="$(/usr/bin/osascript 2>/dev/null <<-'__HEREDOC__'
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set Input to display dialog "Please enter your password:" \
            with title "Password" \
            with icon caution \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
         return text returned of Input as string
      end tell
   end timeout
__HEREDOC__
)"

printf "%s\n" "${input}"


open -a 'Script Editor'

# copy & paste the following snippet into Script Editor 

with timeout of 300 seconds
	tell application "Finder"
		activate
		set Input to display dialog "Please enter your password:" with title "Password" with icon caution default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 295 with hidden answer
		return text returned of Input as string
	end tell
end timeout


#---------------------------------------


unset -v input name
#export name="$(/usr/bin/logname)"
export name="$(/usr/bin/whoami)"

input="$(/usr/bin/osascript 2>/dev/null <<-__HEREDOC__
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set my_name to "${name}"
         set my_pass to display dialog "Enter password for " & quoted form of my_name \
            with title "Login Window" \
            with icon note \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
            return text returned of my_pass as string
      end tell
   end timeout
__HEREDOC__
)"


printf "%s\n" "${input}"

Login window from the command line with Pashua

export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export IFS=$' \t\n'

mkdir -p ~/Applications

ls -ld ~/Applications
stat -x ~/Applications

cd ~/Applications
curl -L -O http://www.bluem.net/files/Pashua.dmg
hdiutil mount Pashua.dmg
cp -R /Volumes/Pashua ~/Applications/Pashua
hdiutil unmount /Volumes/Pashua

cd ~/Applications/Pashua/Examples
cp -p example.sh example.sh.orig   # backup


# some in-place text editing commands to modify ~/Applications/Pashua/Examples/example.sh
# cf. http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed

export FILE="${HOME}/Applications/Pashua/Examples/example.sh"

# replace #!/bin/sh with #!/bin/bash
/bin/ed -s "${FILE}" <<< $'1,1s|bin/sh|bin/bash|\nw'

# to set the encoding to UTF-8 we add: set -- test utf8
/bin/ed -s "${FILE}" <<< $',s|\(.*Manage encoding.*\)|set -- test utf8 # set $2 to "utf8"\\\n\\\n\\1|\nw'

# delete all lines after first regex match /conf="/
/bin/ed -s "${FILE}" <<< $'/conf="/;$d\nw'


# add the following configuration

/bin/cat >> "${FILE}" <<-'EOF'

conf="

# Set transparency: 0 is transparent, 1 is opaque
*.transparency=0.95

# Set window title
*.title = Login Window

*.x = 550
*.y = 300
*.autoclosetime = 300

name.type = textfield
name.label = Please enter your name:
name.width = 280
name.x = 0
name.y = 110

password.type = password
password.label = Please enter your password:
password.width = 280
password.x = 0          
password.y = 45        

# Add a cancel button with default label
cb.type = cancelbutton

";   # end conf


pashua_run "$conf"
#pashua_run "$conf" "utf8"   # alternative to "set -- test utf8" above


if [[ ${cb} -ne 0 ]]; then echo 'Login cancelled!'; exit 1; fi

printf "%s\n" "name = ${name}"

printf "%s\n" "${name}" | ruby -n -e 'p $_.to_s'

# the following command requires #!/bin/bash
# cf. http://www.lugbz.org/pipermail/lugbz-list/2006-December/016360.html
/bin/ed -s <((printf "%s\n" "${name}")) <<< $',l'   

printf "%s\n" "password = ${password}"
printf "%s\n" "cb = ${cb}"

#printf "%s\n" "${password}" | /usr/bin/sudo -S /bin/ls | /usr/bin/head -n 5

/sbin/md5 -qs "${password}"

EOF


# run the script
~/Applications/Pashua/Examples/example.sh

mycmds

help set
set -xv

#echo $-
#shopt -u
#shopt -s
#shopt -s | grep expand_aliases
#shopt -q expand_aliases; echo $?
#help shopt
#shopt -p
#shopt -s expand_aliases   

unalias mycmds1 mycmds2 mycmds3  2>/dev/null
alias mycmds1="declare -F | /usr/bin/awk '{print \$NF}' | /usr/bin/sort"
alias mycmds2="declare -F | /usr/bin/awk '{print $NF}' | /usr/bin/sort"
alias mycmds3='declare -F | /usr/bin/awk "{print $NF}" | /usr/bin/sort'

mycmds1
mycmds2
mycmds3


alias mycmds="alias -p | /usr/bin/awk -F= '{print \$1}' | /usr/bin/sort; \
              declare -F | /usr/bin/awk '{print \$NF }' | /usr/bin/sort"

mycmds

ANSI terminal color chart

1.
#!/bin/bash
#
#   This file echoes a bunch of color codes to the 
#   terminal to demonstrate what's available.  Each 
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a 
#   test use of that color on all nine background 
#   colors (default + 8 escapes).
#
#   Author: Giles Orr
#   URL: http://gilesorr.com/bashprompt/howto/c350.html
#   License: GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation;
#            http://gilesorr.com/bashprompt/howto/a1004.html


T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo

2.
#!/bin/sh
#
#  colors v1.03
#
#  A simple shell script to output an ANSI terminal color chart.
#  It may be useful when trying to customize your ANSI terminal
#  color scheme!
#
#  Written and placed in the public domain by Ian Abbott <ian@abbott.org>
#
#  ANSI terminal color chart generator
#  Author: Ian Abbott
#  License: BSD License, http://www.opensource.org/licenses/bsd-license.php
#  URL: http://www.snippetcenter.org/de/ansi-terminal-color-chart-generator-s1346.aspx

for h in 0 1; do
  echo -e "\\033[0;${h}m\\c"
  case $h in
  0) echo -e "Normal\\c" ;;
  1) echo -e "High\\c" ;;
  esac
  echo -e " intensity foreground (background color in parentheses)\\033[m"
  for f in 0 1 2 3 4 5 6 7; do
    for b in 0 1 2 3 4 5 6 7 8; do
      echo -e "\\033[${h};3${f}\\c"
      if [ $b -lt 8 ]; then
        echo -e ";4${b}m\\c"
      else
        echo -e "m\\c"
      fi
      case $f in
      0) echo -e " BLACK \\c" ;;
      1) echo -e "  RED  \\c" ;;
      2) echo -e " GREEN \\c" ;;
      3) echo -e " YELLOW\\c" ;;
      4) echo -e "  BLUE \\c" ;;
      5) echo -e "MAGENTA\\c" ;;
      6) echo -e "  CYAN \\c" ;;
      7) echo -e " WHITE \\c" ;;
      esac
      case $b in
      8) echo -e "\\033[m" ;;
      *) echo -e " \\033[m \\c" ;;
      esac
    done
  done
  echo -e "\\033[${h}m\\c"
  for b in 0 1 2 3 4 5 6 7 8; do
    case $b in
    0) echo -e "(black)  \\c" ;;
    1) echo -e " (red)   \\c" ;;
    2) echo -e "(green)  \\c" ;;
    3) echo -e "(yellow) \\c" ;;
    4) echo -e " (blue)  \\c" ;;
    5) echo -e "(magenta)\\c" ;;
    6) echo -e " (cyan)  \\c" ;;
    7) echo -e "(white)  \\c" ;;
    8) echo -e " (none)\\c" ;;
    esac
  done
  echo -e "\\033[m\\n"
done

Terminal window commands

# Inspired by: 
# - Re-size and Move with Escape Sequences, http://www.osxfaq.com/tips/unix-tricks/week99/monday.ws
# - Define Aliases and Functions, http://www.osxfaq.com/tips/unix-tricks/week99/tuesday.ws
# - Focus and Dock with Escape Sequences, http://www.osxfaq.com/Tips/unix-tricks/week99/wednesday.ws
# Author: Adrian Mayo, http://101.1dot1.com and http://www.peachpit.com/title/0321374118
# also see: http://en.wikipedia.org/wiki/ANSI_escape_code

function title() { if [[ $# -eq 1 && -n "$@" ]]; then printf "\e]0;${@}\a"; fi; return 0; }  
function title() { if [[ -n "$@" ]]; then printf "\033]0;${1}\007"; fi; return 0; }
function docktw() { printf "\e[2t"; return 0; }
function docktw() { printf "\e[2t"; sleep 5; printf "\e[5t"; return 0; }
function bgtw() { printf "\e[6t"; return 0; }
function bgtw() { printf "\e[6t"; sleep 5; printf "\e[5t"; return 0; }    # background the Terminal window

title 'Hello, world!'
docktw
bgtw


# positive integer test (including zero)
function positive_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1 && test "$@" -ge 0 > /dev/null 2>&1); }

# move the Terminal window
function mvtw() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      printf "\e[3;${1};${2};t"
      return 0
   fi
   return 1
}


# resize the Terminal window
function sizetw() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      printf "\e[8;${1};${2};t"
      /usr/bin/clear
      return 0
   fi
   return 1
}


# full screen
function fscreen() { printf "\e[3;0;0;t\e[8;0;0t"; /usr/bin/clear; return 0; }

# default screen
function dscreen() { printf "\e[8;35;150;t"; printf "\e[3;300;240;t"; /usr/bin/clear; return 0; }

# max columns
function maxc() { printf "\e[3;0;0;t\e[8;50;0t"; /usr/bin/clear; return 0; }

# max rows
function maxr() { printf "\e[3;0;0;t\e[8;0;100t"; /usr/bin/clear; return 0; }

# show number of lines & columns
function lc() { printf "lines: $(/usr/bin/tput lines)\ncolums: $(/usr/bin/tput cols)\n"; return 0; }

# move cursor
function mvc() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      /usr/bin/tput cup "$1" "$2"
      /usr/bin/tput el  # clear to end of line
      #/usr/bin/tput ed
      return 0
   fi
   return 1

}


# wrap lines according to the number of columns of the Terminal window
function wraptw() { 
   declare str var=$(/usr/bin/tput cols)
   if [[ -s /dev/stdin ]]; then str="$(</dev/stdin)"; else str="$@"; fi 
   printf "%s\n" "$str" | /usr/bin/fmt -w $var
   #printf "%s\n" "$str" | /usr/bin/fold -w $(/usr/bin/tput cols)
   return 0
}


long_line="$(/usr/bin/jot -b 'This is a test sentence!' 100 | /usr/bin/tr '\n' ' ')"
printf "%s\n" "$long_line" | wraptw

lc
mvtw 0 0
mvtw 0 300
mvtw 300 300
mvtw 80 140
fscreen
dscreen
maxc
maxr

lc
sizetw 0 0
sizetw 0 50
sizetw 1 50
sizetw 50 50
sizetw 50 500
sizetw 50 150

mvc 2 45
mvc 100 23
mvc 99 3

dscreen


#-----------------------------------------------------


# customize the Terminal window's title bar text

# show the current date as title bar text
declare -x PROMPT_COMMAND='printf "\e]0; $(/bin/date)\a"'   
declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/date "+%a %b %e   %H:%M:%S ")\a"'
declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/date "+%Y-%m-%d   %H:%M:%S ")\a"'

# show current working directory 
declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/pwd) \a"'
declare -x PROMPT_COMMAND='printf "\e]0;$(/bin/pwd | /usr/bin/tr -d "[[:cntrl:]]") \a"'

# show OLDPWD 
declare -x PROMPT_COMMAND='printf "\e]0;$(printf %s "$OLDPWD") \a"'
declare -x PROMPT_COMMAND='printf "\e]0;$(printf %s "$OLDPWD" | /usr/bin/cut -c 1-100) \a"'

export -p | grep -i PROMPT_COMMAND
env | grep -i PROMPT_COMMAND
unset -v PROMPT_COMMAND


# first create environment variable TITLE 
# cf. Prompt Commands, http://www.osxfaq.com/Tips/unix-tricks/week71/wednesday.ws
# Author: Adrian Mayo, http://101.1dot1.com and http://www.peachpit.com/title/0321374118

unset -v TITLE PROMPT_COMMAND
declare -x TITLE="Terminal"
declare -x PROMPT_COMMAND='printf "\e]0;${TITLE}\a"'

# message as title bar text
function message_title() { 
   if [[ $# -eq 1 && -n "$@" ]]; then 
      #TITLE="$@"
      TITLE="$(printf %s "$@" | /usr/bin/cut -c 1-100)"
   fi
   return 0
}

export -f message_title
export -p | grep -i message_title
env | grep -i message_title
#unset -f message_title 

message_title 'Hello, world!'


# show OS information in title bar
function osinfo() { 
   x1="$(/usr/bin/sw_vers -productName)"
   x2="$(/usr/bin/sw_vers -productVersion)"
   x3="$(/usr/bin/sw_vers -buildVersion)"
   x4="$(/usr/bin/arch)"
   TITLE="${x1} - ${x2} - ${x3} - ${x4}"
   return 0
}

export -f osinfo
osinfo


function eval_title() { 
   if [[ -n "$(printf %s "$@") | /usr/bin/egrep '^date|whoami|logname|uname|machine|pwd|hostname$'" ]]; then 
      cmd_output="$(eval "$@")"
      TITLE="$cmd_output"
   fi
   return 0
}
export -f eval_title

eval_title "date"
eval_title "whoami"
eval_title "logname"
eval_title "uname"
eval_title "machine"
eval_title "pwd"
eval_title "hostname"


function date_title() { TITLE="$(/bin/date "+%Y-%m-%d")"; return 0; }
export -f date_title
date_title

function logname_title() { TITLE="$(/usr/bin/logname)"; return 0; }
export -f logname_title
logname_title


# show last command in title bar
help fc
unset -v TITLE PROMPT_COMMAND
declare -x PROMPT_COMMAND='printf %b "\e]0;$(fc -ln | tail -n 1 | cut -c 2-150 | tr -d "\\")\a"'


# show current command in title bar
help history
unset -v TITLE PROMPT_COMMAND
declare -x PROMPT_COMMAND='printf %b "\e]0;$(history 1 | /usr/bin/cut -c 8-150 | tr -d "\\")\a"'
declare -x PROMPT_COMMAND='printf %b "\e]0;$(history 1 | sed -E "s/^[[:space:]]*[[:digit:]]+[[:space:]]+(.*)$/\\1/" | cut -c 1-150 | tr -d "\\")\a"'


#-----------------------------------------------------


# increase font size
function ifont() { 
/usr/bin/osascript <<__END__
    tell application "System Events" to tell process "Terminal" to keystroke "+" using command down
__END__
  return 0 
}

ifont


# decrease font size
function dfont() { 
/usr/bin/osascript <<__END__
    tell application "System Events" to tell process "Terminal" to keystroke "-" using command down
__END__
  return 0 
}

dfont


# hide all Terminal windows
function hideall() { 
   /usr/bin/osascript -e 'tell application "System Events" to set visible of some item of ( get processes whose name = "Terminal" ) to false'
   return 0
}

hideall


# create a new Terminal window and cd to the same current working directory
# cf. http://justinfrench.com/index.php?id=231

unset -f newin

function newin() {
   /bin/pwd | /usr/bin/pbcopy
   /usr/bin/open -a Terminal
   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "cd \"$(/usr/bin/pbpaste)\"; echo \" \" | /usr/bin/pbcopy; /usr/bin/clear"'
   return 0 
}

cd ~/Desktop
newin

Screensaver as desktop background

# cf. http://digg.com/apple/Top_15_Terminal_Commands_for_Hidden_Mac_OS_X_Settings

function sadb() {
# cf. /usr/bin/locate '*ScreenSaverEngine'
/usr/bin/screen -d -m /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background 
return 0
}

function sadb_stop() {
#/usr/bin/killall -d ScreenSaverEngine
#/usr/bin/killall -v ScreenSaverEngine
/usr/bin/killall -HUP ScreenSaverEngine
return 0
}

sadb
sleep 30
sadb_stop

Flushing routing tables on Mac OS X

# cf. http://ola-bini.blogspot.com/2008/05/faulty-routes-on-macos-x.html
/usr/bin/sudo /sbin/route flush

Using PlistBuddy to customize syslogd


# Inspired by: http://www.maciverse.com/the-case-of-the-slow-mac-and-how-to-fix-it.html

# cf. man syslog, man syslogd and http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/syslogd.8.html
/usr/bin/open -a Console   # system.log
/usr/bin/tail -n 50 /private/var/log/system.log

help declare
declare -x sudo=/usr/bin/sudo plistbuddy=/usr/libexec/PlistBuddy defaults=/usr/bin/defaults
/usr/bin/env | /usr/bin/grep -E 'sudo=|plistbuddy=|defaults='

# create a symbolic link (on Mac OS X 10.4)
help test
/usr/bin/locate PlistBuddy
if [[ ! -e /usr/libexec/PlistBuddy ]]; then $sudo /bin/ln -s "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy" /usr/libexec/PlistBuddy; fi

# make a backup
$sudo /bin/cp -p /System/Library/LaunchDaemons/com.apple.syslogd.plist /System/Library/LaunchDaemons/com.apple.syslogd.plist.orig

# cf. http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html
$plistbuddy -h   

$plistbuddy -c Print /System/Library/LaunchDaemons/com.apple.syslogd.plist
$plistbuddy -c "Print :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist

# compare
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd ProgramArguments

$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

$sudo $plistbuddy -c "Delete :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments array" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:0 string '/usr/sbin/syslogd'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:1 string '-c'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:2 integer 3" /System/Library/LaunchDaemons/com.apple.syslogd.plist

# Mac OS X 10.5
#$sudo $plistbuddy -c "Add :ProgramArguments:3 string '-a'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
#$sudo $plistbuddy -c "Add :ProgramArguments:4 string '-db_max'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
#$sudo $plistbuddy -c "Add :ProgramArguments:5 integer 5000000" /System/Library/LaunchDaemons/com.apple.syslogd.plist

$plistbuddy -c "Print :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd ProgramArguments
$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

# reset com.apple.syslogd.plist
#if [[ -n $(/usr/bin/sw_vers -productVersion | /usr/bin/grep '^10.5') ]]; then exit; fi   # exit if on Mac OS X 10.5.x
$sudo /bin/rm -f /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo /bin/cp -p /System/Library/LaunchDaemons/com.apple.syslogd.plist.orig /System/Library/LaunchDaemons/com.apple.syslogd.plist

$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

# restart syslogd
#$sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.syslogd.plist 2>/dev/null
#/bin/sleep 3
#$sudo /bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.syslogd.plist 2>/dev/null
#$sudo /usr/bin/killall -HUP syslogd   # alternative