Unformatted text preview:

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 ShellTo change your default shell use the chsh utilityTo examine your default shell, type:echo $SHELLmweeks@carmaux:~$ echo $SHELL/bin/bashCORE Shell FunctionalityBuilt-in commandsScriptsVariables (local, environment)RedirectionWildcardsCORE Shell FunctionalityPipesSequences (conditional, unconditional)SubshellsBackground processingCommand substitutionInvoking the ShellA 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 | \ lpls (list files), ps (process info), ul (underline),\ continues line, lp (send to printer)Built-in commandsMost 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: echocdBuilt-in commandsecho 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 dirMetacharactersOutput redirection> writes standard output to file>> appends standard output to fileInput redirection< reads std. input from file<<tok read std. input until tokMetacharactersFile-substitution wildcards:* matches 0 or more characters? matches any single character[...] matches any character within bracketsCommand substitution:`command` replaced by the output of commande.g. echo `ls`Metacharacters| Pipesend output of one process to the input of anothere.g. list files, then use wordcount to count linesls | wc -lthis effectively counts the filesMetacharacters; Used to sequence commandsConditional 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 followsRedirectionThe shell redirection facility allows you tostore the output of a process to a fileuse the contents of a file as input to a processExamples:cat x1.c > y.ccat x2.c >> y.cmail tony < hiMomThe <<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 filenamecauses 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 SubstitutionA 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 systemSequencesCommands or pipelines separated by semi-colonsEach 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.captureSequencesConditional sequences: $ cc myprog.c && a.out $ cc myprog.c || echo compilation failedIn 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 commandsCommands can be grouped by putting them within parenthesesa sub shell is created to execute the grouped commandsExample: $ (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 processingAn & sign at end of a simple command,or pipeline, sequence of pipelines, or a group of commandsStarts a sub-shell commands are executed as a background processdoes not take control of the keyboardA process id is displayed when it beginsBackground processingRedirect the output to a file (if desired)prevents background output on terminalBackground process cannot read from standard input If they attempt to read from standard input; they terminate.Shell Programs/ScriptsShell commands may be stored in a text file for executionUse the


View Full Document

GSU CSC 3320 - chapter4

Download chapter4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view chapter4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view chapter4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?