Scripting

From Wikifications
Revision as of 19:28, 30 December 2005 by Dre (Talk | contribs)

Jump to: navigation, search

Theory

Redirection:

>      Redirect standard output
2>     Redirect standard error
2>&1   Redirect standard error to standard output
<      Redirect standard input
|      Pipe standard output to another command
>>     Append to standard output
2>&1|  Pipe standard output and standard error to another command
|&     Same as above

Basic shell scripting idioms

simple for loop:

for file in `ls` 
do
mv $file $file.copy 
done

basic if statement:

if [ 1 = 1 ] ; then echo "yes" ; else echo "no" ; fi

tests (for cli args):

if ( ! [ $1 ] || [ ! $2 ] ) ; then echo "yup" ; else echo "nope" ; fi

Practice

Disk Stats (zsh, all one line)

x=0 ; df -t hfs,afpfs | grep "/" | awk '{print $2}' | sed 's/G//g' | while read line ;\
do x=($line + $x); done ; echo "($x) / 1024 / 1024 / 2" | bc

Get console idle time

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))