Professional Documents
Culture Documents
UNIX / Linux Shell Cheat Sheet: File Manipulation Test Operators Variable Substitution
UNIX / Linux Shell Cheat Sheet: File Manipulation Test Operators Variable Substitution
shtml
UNI
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
find matching lines get field with delimiter get output of command
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*
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:
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