You are on page 1of 1

http%MMsteve5par/er.orgMshMsh.

shtml

v1.1 7 Aug 2007

UNI

/ !inu" #hell $heat #heet

File %anipulation
> file >> file >file 2> 1 + file a,! create (overwrite) file append to file !oth output and errors to file read from file pipe output from -a- as input to -!-

&est 'perators
if N )"1* 5lt )"3* O. then # do something fi Numeri) &ests lt gt read te1t file line !3 line e0 ne ge le less than greater than e0ual to not e0ual greater or e0ual less or e0ual

(ariable #ubstitution
"#$%5def' "#$%8def' "#$%&err' "$( or )def* if unset "$ (set to )def* if unset) "$( or )err* if unset

$onditional *"e)ution
c1 ,, c2 c1 c2 run c1. if it fails( run c2 run c1. if it wor/s( run c2

$ommon $onstru)ts
while read f do echo )2ine is "f* done + file " grep foo m3file afoo foo foo!ar " cut 5d% 5fQ MetcMpasswd :il!ert foo8PlsP

$ommon utilities and s+it)hes


ls 5lHr ls 5ltr ls 5lh du 5s/ > sort 5n ps 5ef wget 672 time cmd touch file read 1 cmd , R tee file.t1t nice cmd list files( !iggest last list files( newest last human5reada!le filesi4es director3 si4es (slow) sort numericall3 (not alpha) list m3 commands download 672 stopwatch on PcmdP create file read )1* from /e3!oard cmd output to stdout and also to file.t1t run cmd with low priorit3

find matching lines get field with delimiter get output of command

File &ests nt newer than d f r w 1 is a director3 is a file reada!le writea!le e1ecuta!le

#tring &ests 8 e0ual to 4 n 4ero length not 4ero length

case "foo in a) echo )foo is A* .. !) echo )foo is T* .. >) echo )foo is not A or T* .. esac dou!leit() # e1pr "1 R> 2 ' dou!leit B # returns U function declaration and calling s3nta1 A for loop for i in > do echo )File is "i* done iterates through its input (which is su!Gect to glo!!ing) case is a good wa3 to avoid iterating through man3 ifMelifMelifMelif constructs.

Net+orking
ifconfig 5a netstat 5r ssh uShost scp file.t1t R uShost% list all networ/ interfaces show routers log in to host as user )u* cop3 file.t1t to host as user )u*

!ogi)al &ests ,, < logical A9: logical ;7 logical 9;=

Argument (ariables
"0 "1 "2 ... "@ "> "# program name 1st argument 2nd argument ... @th argument all arguments 9o. of arguments

,eneral Admin
less file alias l8-ls 5ltar cf t.tar R list?of?files cal B 1@7B df 5h truss 5p CD: displa3 file( page !3 page create )l* as alias for )ls 5l* create a tar archive t.tar from the listed dirsMfiles displa3 a calendar (Aar 7B) show dis/ mounts show s3scalls of CD:

Files: $ontents / Attributes


find . 5si4e 10/ 5print find . 5name )>.t1t* 5print find Mfoo 5t3pe d 5ls three8Pe1pr 1 V 2P echo Wscale 8 Q . Q121 M 102X* , !c egrep )(foo,!ar)* file files over 10E! find te1t files ls all directories under Mfoo simple maths !etter maths find )foo* or )!ar* in file print the Qth word of each line replace )foo* in file with )!ar*

Useful (ariables
"DFH "& "HIJ22 2A9K Dnternal File Heparator return code from last program what shell is running this script& 2anguage. L is 6H Jnglish aw/ -# print "Q '- file sed sMfooM!arMg file

A full PDF and online tutorial is available at http://steve-parker.org/sh/sh.shtml

You might also like