Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Chapter 4The UNIX Shells (Bourne shell, Korn shell, C shell)Graham Glass and King Ables, UNIX for Programmers and Users,Third Edition, Pearson Prentice Hall, 2003.Original Notes by Raj SunderramanConverted to presentation and updated by Michael WeeksWhich ShellTo change your default shell use the chsh utilityTo examine your default shell, type:echo $SHELLmweeks@carmaux:~$ echo $SHELL/bin/bashCORE Shell FunctionalityBuilt-in commandsScriptsVariables (local, environment)RedirectionWildcardsCORE Shell FunctionalityPipesSequences (conditional, unconditional)SubshellsBackground processingCommand substitutionInvoking the ShellA shell is invoked, either automatically upon login, or manually from the keyboard or scriptWhat does the shell do?The following takes place:(1) reads a special startup file (.cshrc for csh in the user's home directory) and executes all the commands in that file(2) displays a prompt and waits for a user command(3) If user enters CTRL-D (end of input) the shell terminates, otherwise it executes the user command(s)User Commands $ ls $ ps -ef | sort | ul -tdumb | lp $ ls | sort | \ lpls (list files), ps (process info), ul (underline),\ continues line, lp (send to printer)Built-in commandsMost Unix commands invoke utility programs stored in the file hierarchy (ex. ls, vi etc)The shell has to locate the utility (using PATH variable)Shells have built-in commands, ex: echocdBuilt-in commandsecho arguments$ echo Hi, How are you?Hi, How are you?echo by default appends a new line (to inhibit new line use -n option in csh)cd dirMetacharactersOutput redirection> writes standard output to file>> appends standard output to fileInput redirection< reads std. input from file<<tok read std. input until tokMetacharactersFile-substitution wildcards:* matches 0 or more characters? matches any single character[...] matches any character within bracketsCommand substitution:`command` replaced by the output of commande.g. echo `ls`Metacharacters| Pipesend output of one process to the input of anothere.g. list files, then use wordcount to count linesls | wc -lthis effectively counts the filesMetacharacters; Used to sequence commandsConditional execution|| execute command if previous one fails&& execute command if previous one succeedsMetacharacters(...) Group commands& Run command in background# Comment rest of characters ignored by shell$ Expand the value of a variable\ Prevent special interpretation of character that followsRedirectionThe shell redirection facility allows you tostore the output of a process to a fileuse the contents of a file as input to a processExamples:cat x1.c > y.ccat x2.c >> y.cmail tony < hiMomThe <<tok redirection is almost exclusively used in shell scripts (will see this later)Filename substitution$ ls *.c # list .c files$ ls ?.c # list files like a.c, b.c, 1.c, etc$ ls [ac]* # list files starting with a or c$ ls [A-Za-z]* # list files beginning with a letter$ ls dir*/*.c # list all .c files in directories starting with dirPipes$ command1 | command2 | command3$ lsppp00* ppp24* ppp48* ppp72*$ ls | wc -w4Pipes$ head -4 /etc/passwdroot:fjQyH/FG3TJcg:0:0:root:/root:/bin/shbin:*:1:1:bin:/bin:daemon:*:2:2:daemon:/sbin:adm:*:3:4:adm:/var/adm:$ cat /etc/passwd | awk -F: '{print $1}' | sortadmbindaemonrajtee utility$ tee -ia filenamecauses standard input to be copied to file and also sent to standard output. -a option appends to file-i option ignores interruptstee utility $ who raj tty1 Jun 19 09:31 naveen ttyp0 Jun 19 20:17 (localhost) $ who | tee who.capture | sort naveen ttyp0 Jun 19 20:17 (localhost) raj tty1 Jun 19 09:31 $ more who.capture raj tty1 Jun 19 09:31 naveen ttyp0 Jun 19 20:17 (localhost)Command SubstitutionA command surrounded by grave accents (`) is executed and its standard output is inserted in the command's place in the command line.$ echo today is `date`today is Sat Jun 19 22:23:28 EDT 2007$ echo there are `who | wc -l` users on the systemthere are 2 users on the systemSequencesCommands or pipelines separated by semi-colonsEach command in a sequence may be individually I/O redirected.Example:$date; pwd; ls$date > date.txt; pwd > pwd.txt; lsSequences $ date; pwd; ls Sat Jun 19 22:33:19 EDT 2007 /home/raj/oracle jdbc/ ows/ proc/ sql/ sqlj/ who.capture $ date > date.txt; pwd > pwd.txt; ls date.txt jdbc/ ows/ proc/ pwd.txt sql/ sqlj/ who.captureSequencesConditional sequences: $ cc myprog.c && a.out $ cc myprog.c || echo compilation failedIn a series of commands separated by &&, the next command is executed if the previous one succeeds (returns an exit code of 0)In a series of commands separated by || the next command is executed if the previous one fails (returns an exit code of non-zero)Grouping commandsCommands can be grouped by putting them within parenthesesa sub shell is created to execute the grouped commandsExample: $ (date; ls; pwd) > out.txt $ more out.txtGrouping commands $ (date; ls; pwd) > out.txt $ more out.txt Sat Jun 19 22:40:43 EDT 2007 date.txt jdbc/ out.txt ows/ proc/ pwd.txt sql/ sqlj/ who.capture /home/raj/oracleBackground processingAn & sign at end of a simple command,or pipeline, sequence of pipelines, or a group of commandsStarts a sub-shell commands are executed as a background processdoes not take control of the keyboardA process id is displayed when it beginsBackground processingRedirect the output to a file (if desired)prevents background output on terminalBackground process cannot read from standard input If they attempt to read from standard input; they terminate.Shell Programs/ScriptsShell commands may be stored in a text file for executionUse the
View Full Document