Difference between revisions of "Scripting"

From Wikifications
Jump to: navigation, search
Line 9: Line 9:
 
  2>&1|  Pipe standard output and standard error to another command
 
  2>&1|  Pipe standard output and standard error to another command
 
  |&    Same as above
 
  |&    Same as above
 +
 +
'''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))
 +
 +
'''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

Revision as of 19:26, 30 December 2005

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

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

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