ChapThe UNI(Bourne shell, KoGraham Glass aGraham Glass aUNIX for ProgramThird Edition, PearsoOriginal Notes by Ctdt tConverted to presentMichaelpter 4X Shells orn shell, C shell)and King Ablesand King Ables, mmers and Users,n Prentice Hall, 2003.Raj Sunderramantti d dtdbtation and updated by l Weeks● No programming langunot even a single best lglanguages well suited oftilfor particular purposes.uage is perfect. There is language; there are only gg; yor perhaps poorly suited .--Herbert MayerWhichWhich● To change your defaultDirectly type command●Directly type commandchange to another shel● To examine your defau– echo $SHELL$ echo $SHELL/bin/bashh Shellh Shellt shell use the chsh utilitysh csh ksh bash to sh, csh, ksh, bash to lult shell, type:Shell fB ildiBuild-inCommandsScriptsVariablesRedirectionWildcardsScriptsVariablesRedirectionWildcardsLocal EnvironmentfunctionsCommandPipesSequencesSubshellsBackgroundCommandsubstitutionPipesSequencesSubshellsBackgroundprocessingConditional UnconditionalCORE ShellCORE Shell ● Built-in commandsScripts●Scripts●Variables (local, enviroVariables (local, enviro● Redirection● WildcardsFunctionalityFunctionalitynment)nment)CORE ShellCORE Shell ● PipesSequences (conditiona●Sequences (conditiona●SubshellsSubshells● Background processing● Command substitutionFunctionalityFunctionalityal unconditional)al, unconditional)gInvokingInvoking ●A shell is invoked, eitheautomatically upon login–automatically upon login– manually from the keybthe Shellthe Sheller norn, or oard or scriptWhat does tWhat does t● The following takes pla(1) reads a special start–(1) reads a special startuser's home directory) ad i th t fil (commands in that file (–(2) displays a prompt an() p y p p– (3) If user enters CTRL-tit thiitterminates, otherwise it command(s)he shell do?he shell do?ace:tup file ( cshrc for csh in thetup file (.cshrc for csh in the and executes all the bh).bashrc )nd waits for a user command-D (end of input) the shell tthexecutes the userUser ComUser Com● ls (list files), ps (proces\continues line lp (sen●\continues line, lp (sen$ ls$ ps -ef | sort | lp$ ls | sort | \lppmmandsmmandsss info),nd to printer)nd to printer)Builtin coBuilt-in co● Most Unix commands istored in the file hierarc● The shell has to locate variable)Sh ll h b ilti●Shells have built-in com–echoecho– cdommandsommandsnvoke utility programs chy (ex. ls, vi etc)y(,)the utility (using PATHdmmands, ex:Builtin coBuilt-in co● echo arguments$ echo Hi How are you–$ echo Hi, How are you– Hi, How are you?● echo by default appendliti inew line use -n option i●cd dir●cd dirommandsommandsu?u?ds a new line (to inhibit ih)in csh)MetachaMetacha● Output redirection>writes standa●> writes standa● >>appends standardappends standard● Input redirection● < reads std. input fr● <<tok read std. inpuaractersaractersard output to fileard output to filed output to filed output to filerom fileut until tokMetachaMetacha● File-substitution wildca*matches 0 or m–matches 0 or m– ? matches any si– [...] matches any chCdbii●Command substitution:–`command`replaced by–command replaced by– e.g. echo `ls`aractersaractersrds:more charactersmore charactersngle characterharacter within brackets:y the output of commandy the output of commandMetachaMetacha● | Pipesend output of one proc●send output of one procanother– e.g. list files, then use w● ls | wc -l– this effectively counts tharactersaracterscess to the input ofcess to the input of wordcount to count lineshe filesMetachaMetacha● ; Used to sequenceConditional execution●Conditional execution–|| execute comma||– && execute command iaractersaracterse commandsand if previous one failspf previous one succeedsMetachaMetacha● (...) Group commands&Run command in●&Run command in ●#Comment# Comment – rest of characters ignore● $ Expand the value●\Prevent special inthat followsthat followsaractersaracterssbackgroundbackgrounded by shelle of a variablenterpretation of characterRedireRediref● The shell redirection facility allo– store the output of a proces– use the contents of a file asExamples:●Examples:– cat x1.c > y.c– cat x2.c >> y.cmail tony < hiMom–mail tony < hiMom● The <<tok redirection is almost( ill thi l t )(will see this later)ectionectionows you toss to a files input to a processt exclusively used in shell scriptsFilename sFilename s● $ ls *.c # list $ls ? c# list●$ ls ?.c# list ●$ls [ac]*# list$ ls [ac]# list ● $ ls [A-Za-z]* # list files● $ ls dir*/*.c # list iihdistarting with dirsubstitutionsubstitution.c filesfiles like a c b c 1 c etcfiles like a.c, b.c, 1.c, etcfiles starting with a or cfiles starting with a or cs beginning with a letterall .c files in directoriesPipPip● $ command1 | comman$ lsppp00*ppp24*ppp48*pppppp00 ppp24 ppp48 ppp$ ls | wc -w4pespesnd2 | command372*72PipPip$ cat /etc/passwd | awk -F: admbibindaemonrajrajpespes'{print $1}' | sorttee utee u● $ tee -ia filenamecauses standard input t●causes standard input talso sent to standard o– -a option appends to file– -i option ignores interruputilityutilityto be copied to file andto be copied to file and utput. eptstee utee u$ who > who.capture | sort$h| h |$ who | tee who.capture | so$ more who capture$ more who.captureutilityutilityortCommand SCommand S●A command surroundeexecuted and its standaexecuted and its standathe command's place in$ echo today is `date`today is Sat Jun 19 22:23:28 Ey$ echo there are `who | wc -l` there are 2 users on the systemSubstitutionSubstitutioned by grave accents (`) is ard output is inserted inard output is inserted in n the command line.EDT 2007users on the systemmSequeSeque● Commands or pipelinescolons● Each command in a seindividually I/O redirectEl●Example:–$date; pwd; ls$date; pwd; ls– $date > date.txt; pwd > encesencess separated by semi-equence may be ed.pwd.txt; lsSequeSeque$ date; pwd; lsSat Jun 19 22:33:19 EDT 20/home/raj/oraclejdbc/ ows/ proc/ sql/ s$d d d d$ date > date.txt; pwd > pwddate.txt jdbc/ ows/ procwho capturewho.captureencesences007sqlj/ who.capturedld.txt; lsc/ pwd.txt sql/ sqlj/SequeSequeC diti l●Conditional sequences:●$ cc myprog.c && a.out$ypg● $ cc myprog.c || echo com● In a series of commands scommand is executed if th(returns an exit code of 0)In a series of commands s●In a series of commands scommand is executed if than exit code of
View Full Document