12.07.2015 Views

Bourne Shell Quick Reference Card - Hooman B . com

Bourne Shell Quick Reference Card - Hooman B . com

Bourne Shell Quick Reference Card - Hooman B . com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Bourne</strong> <strong>Shell</strong> <strong>Quick</strong> <strong>Reference</strong> <strong>Card</strong>I. Introduction to <strong>Shell</strong> ScriptsA. The shell is the program that you run when you log inB. It is a <strong>com</strong>mand interpreterC. There are three standard shells - C, Korn and <strong>Bourne</strong>D. <strong>Shell</strong> prompts users, accepts <strong>com</strong>mand, parses, theninterprets <strong>com</strong>mandE. Most <strong>com</strong>mon form of input is <strong>com</strong>mand line inputcat file1 file2 file3F. Most <strong>com</strong>mands are of the format<strong>com</strong>mand [- option list] [argumentlist]G. Redirection and such1. < redirect input from standard input2. > redirect output from standard output3. >> redirect output and append4. | "pipes" output from one <strong>com</strong>mand to anotherls -l | more5. tee "pipes" output to file and standard outls -l | tee rpt2 | moreH. Entering <strong>com</strong>mands1. Multiple <strong>com</strong>mands can be entered on the same line ifseparated by ;2. Command can span multiple lines if \R is typed at theend of each line except the last (R stands forcarriage return, i.e. ENTER). This is escapesequence.I. Wild card characters can be used to specify file namesin <strong>com</strong>mands1. * 0 or more characters2. ? one character of any kind3. [, , ] list of characters to match single characterJ. Simplest scripts <strong>com</strong>bine <strong>com</strong>mands on single linelikels -l | tee rpt2 | moreK. Slightly more <strong>com</strong>plex script will <strong>com</strong>bine <strong>com</strong>mandsin a file1. Use any text editor to create file, say my_sc2. Type <strong>com</strong>mands into file, one per line (unless you use; to seperate)3. Save file4. Make file readable and executable (more later on this)chmod a+rx my_sc5. run script by entering path to file./my_scWe will make this a little easier laterL. See examples 1 and 2II.III.IV.VariablesA. The statment name=value creates and assigns value tovariableSUM=12B. Traditional to use all upper case characters for namesC. Access content of variable by preceding name with $echo $SUMD. Arguments go from right to leftE. Results of <strong>com</strong>mands can be assigned to variablesSYS=`hostname`F. Strings are anything delimited by ""G. Variables used in strings are evaluatedH. See example 3I. System/standard variables1. Command line argumentsAccessed by $1 through $9 for the first 9 <strong>com</strong>mandline arguments. Can access more by using the shift<strong>com</strong>mand. This makes $1 .. $9 reference <strong>com</strong>mandline arguments 2-10. It can be repeated to access along list of arguments.2. $# number of arguments passed on the <strong>com</strong>mand line3. $- Options currently in effect (supplied to sh or to set4. $* all the <strong>com</strong>mand line arguments as one long doublequoted string5. $@ all the <strong>com</strong>mand line arguments as a series ofdouble quoted strings6. $? exit status of previous <strong>com</strong>mand7. $$ PID ot this shell's process8. $! PID of most recently started background job9. $0 First word, that is, name of <strong>com</strong>mand/scriptConditional Variable SubstitutionA. ${var:-string} Use var if set, otherwise use stringB. ${var:=string} Use var if set, otherwise use stringand assign string to varC. ${var:?string} Use var if set, otherwise print stringand exitD. ${var:+string} Use string if var if set, otherwiseuse nothingConditionalA. The condition part can be expressed two ways. Either astest conditionor[ condition ]where the spaces are significant.B. There are several conditions that can be tested for1. -s file file greater than 0 length2. -r file file is readable3. -w file file is writable4. -x file file is executable5. -f file file exists and is a regular file6. -d file file is a directory7. -c file file is a character special file8. -b file file is a block special file9. -p file file is a named pipe10. -u file file has SUID set11. -g file file has SGID set12. -k file file has sticky bit set13. -z string length of string is 014. -n string length of string is greater than 015. string1 = string2 string1 is equal to string216. string1 != string2 string1 is different fromstring217. string string is not null18. int1 -eq int2 integer1 equals integer219. int1 -ne int2 integer1 does not equal integer220. int1 -gt int2 integer1 greater than integer221. int1 -ge int2 integer1 greater than or equal tointeger222. int1 -lt int2 integer1 less than integer223. int1 -le int2 integer1 less than or equal to integer224. !condition negates (inverts) condition25. cond1 -a cond2 true if condition1 and condition2 areboth true26. cond1 -o cond2 true if either condition1 orcondition2 are true27. \( \) used to group <strong>com</strong>plex conditionsV. Flow ControlA. The if statementif conditionthen<strong>com</strong>mandselse<strong>com</strong>mandsfiB. Both the while and until type of loop structures aresupportedwhile conditiondo<strong>com</strong>mandsdoneuntil conditiondo<strong>com</strong>mandsdone


VI.C. The case statement is also supportedcase string inpattern1)<strong>com</strong>mands;;pattern2)<strong>com</strong>mands;;esacThe pattern can either be an integer or a singlequoted stringThe * is used as a catch-all default patternD. The for <strong>com</strong>mandfor var [in list]do<strong>com</strong>mandsdonewhere either a list (group of double quoted strings)is specified, or $@ is usedOther CommandsA. Output1. Use the echo <strong>com</strong>mand to display data2. echo "This is some data" will output thestring3. echo "This is data for the file =$FILE" will output the string and expand thevariable first. The output from an echo <strong>com</strong>mandis automatically terminated with a newline.B. Input1. The read <strong>com</strong>mand reads a line from standard input2. Input is parsed by whitespace, and assigned to eachrespective variable passed to the read <strong>com</strong>mand3. If more input is present than variables, the lastvariable gets the remainder4. If for instance the <strong>com</strong>mand was read a b c andyou typed "Do you Grok it" in response, thevariables would contain $a="Do", $b="you"$c="Grok it"C. Set the value of variables $1 thru $n1. If you do set `<strong>com</strong>mand`, then the results for the<strong>com</strong>mand will be assigned to each of the variables$1, $2, etc. parsed by whitespaceD. Evaluating expressions1. The expr <strong>com</strong>mand is used to evaluate expressionsVII.VIII.2. Useful for integer arithmetic in shell scripts i=`expr$i +1`E. Executing arguments as shell <strong>com</strong>mands1. The eval <strong>com</strong>mand executes its arguments as a shell<strong>com</strong>mand<strong>Shell</strong> functionsA. General format isB. function_name ()C. {D. <strong>com</strong>mandsE. }MiscellaneousA. \n at end of line continues on to next lineB. Metacharacters1. * any number of characters2. ? any one character3. [,] list of alternate characters for one character positionC. Substitution1. delimit with `` (back quote marks, generally top leftcorner of keyboard)2. executes what is in `` and substitutes result in stringD. Escapes1. \ single character2. ' groups of characters3. " groups of characters, but some special charactersprocessed ($\`)E. <strong>Shell</strong> options1. Restricted shell sh -ra. can't cd, modify PATH, specify full path names orredirect outputb. should not allow write permissions to directory2. Changing shell optionsa. Use set option +/- to turn option on/offb. e interactive shellc. f filename substitutiond. n run, no execution of <strong>com</strong>mandse. u unset variables as errors during substitutionf. x prints <strong>com</strong>mands and arguments during executionExamplesSingle Line Script#! bin/sh# Script lists all files in currentdirectory in decending order by sizels -l | sort -r -n +4 -5Multiline Script#!/usr/bin/ksh# Lists 10 largest files in currentdirectory by sizels -l > /tmp/f1sort -r -n +4 -5 /tmp/f1 > /tmp/f2rm /tmp/f1head /tmp/f2 > /tmp/f3rm /tmp/f2more /tmp/f3rm /tmp/f3#!/usr/bin/ksh# Uses variables to store data from<strong>com</strong>mandsSYS=`hostname`ME=`whoami`W="on the system"echo "I am $ME $W $SYS"Copyright © 2002 <strong>Hooman</strong> Baradaranhttp://www.hoomanb.<strong>com</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!