Linux ShellsBook Chapter 5What is a shell?Examples:bash Bourne Again shellksh Korn shelltcsh C shell 1Linux ShellsLinux default shell /bin/bashHow do I know what shell I am running?echo $SHELLenv2Shell OperationsShell invocation sequence1. read special startup file containing initialization info2. display prompt and wait for user command3. execute commandif “end of file” exit shellotherwise execute command entered3Shells operationsShell command exampleslsps -ef | sort | ul -tdumb | lp“\” serves as line extension character e.g.: echo this is a long command that does \ not fit in one line4Commands - executablesWhere are the shell commands?most commands invoke utility programsshell executes executable stored in file systeme.g., ls (\bin\ls)5Commands - built-inWhere are the shell commands?other commands are built-in, e.g.,echo [option]... [string] which displays line of text cdbash built-ins man cd outputsbash, :, ., [, alias, bg, bind, break, builtin, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, unalias, unset, wait - bash built-in commands, see bash(1)67Shell MetacharactersRedirectionOutput RedirectionStore output of process in fileecho hello world > file writes string into fileecho more words >> file append string to fileInput Redirectionuse contents of file as input8Filename SubstitutionShell wildcards* matches any string including empty string? matches any single character[..] matches any one of the chars in bracketswhat do the following examples display?ls -l *.cls [A-Za-z]*ls */*.c9PipesUse output of one command as input to anotherpipe output of one process to the input of anotherexamplels |wc -w cat /etc/passwd | gawk -F: '{ print $1 }' | sort10Pipesexample: tee - read from standard input and write to standard output and files$ who | tee who.capture | sortables pts/6 May 3 17:54 (gw.waterloo.com)glass pts/0 May 3 18:49 (blackfoot.utdall)posey pts/2 Apr 23 17:44 (:0.0)posey pts/4 Apr 23 17:44 (:0.0)$ cat who.capture ...look at the captured data.glass pts/0 May 3 18:49 (blackfoot.utdalla)posey pts/2 Apr 23 17:44 (:0.0)posey pts/4 Apr 23 17:44 (:0.0)ables pts/6 May 3 17:54 (gw.waterloo.com)11Command SubstitutionA command surrounded by grave accents (`, back quotes) is executed and its output (after evaluation) is inserted in the command in its place. Any newlines in the output are replaced by spaces12-bash-3.2$ echo the date today is `date` the date today is Mon Sep 20 10:44:18 PDT 2010-bash-3.2$ -bash-3.2$ echo there are `who | wc -l` users on the systemthere are 3 users on the system-bash-3.2$SequencesA series of simple commands or pipelines separated by semicolons is executed in sequence, from left to right.13-bash-3.2$ who ; date; pskrings pts/0 2010-09-20 10:43 (star.cs.uidaho.edu)jeffery pts/1 2010-09-14 11:50 (clint2.cs.uidaho.edu)jeffery pts/3 2010-09-08 13:02 (75.87.248.45)Mon Sep 20 10:51:47 PDT 2010 PID TTY TIME CMD26826 pts/0 00:00:00 bash26941 pts/0 00:00:00 psConditional SequencesEvery Linux process terminates with an exit value0 means normal executionnonzero indicates failurebuilt-in commands return 1 if they fall14Conditional SequencesUsing exit valuesIf you specify a series of commands separated by && tokens, the next command is executed only if the previous command returns an exit code of 0.If you specify a series of commands separated by || tokens, the next command is executed only if the previous command returns a nonzero exit codeThe && and || metacharacters mirror the operation of their counterpart C operators.-bash-3.2$ gcc cpu.c && ./a.out -bash-3.2$ gcc myprog.c || echo compilation failed.15Background ProcessingA command followed by the & metacharacter will be executed in the backgroundexecute several programs in backgroundlook at them using ps or topbring them to the foreground use bg, fg, ^Z16Background ProcessingA process running in the background may still output to the screen (stdout)different ways to deal with that, e.g.redirect to fileredirect to dummy device, /dev/nullmail to yourself, e.g. find . -name a.c -print | mail glass
View Full Document