📄 t3
字号:
substitution..SH3.5\ Error\ handling.LPThe treatment of errors detected bythe shell depends on the type of errorand on whether the shell is beingused interactively.An interactive shell is one whoseinput and output are connectedto a terminal (as determined by\fIgtty\fP (2)).A shell invoked with the \fB\(mii\fPflag is also interactive..LPExecution of a command (see also 3.7) may failfor any of the following reasons..IP \(buInput output redirection may fail.For example, if a file does not existor cannot be created..IP \(buThe command itself does not existor cannot be executed..IP \(buThe command terminates abnormally,for example, with a "bus error"or "memory fault".See Figure 2 below for a complete listof UNIX signals..IP \(buThe command terminates normallybut returns a non-zero exit status..LPIn all of these cases the shellwill go on to execute the next command.Except for the last case an errormessage will be printed by the shell.All remaining errors cause the shellto exit from a command procedure.An interactive shell will returnto read another command from the terminal.Such errors include the following..IP \(buSyntax errors.e.g., if \*(ZZ then \*(ZZ done.IP \(buA signal such as interrupt.The shell waits for the currentcommand, if any, to finish execution andthen either exits or returns to the terminal..IP \(buFailure of any of the built-in commandssuch as \fIcd.\fP.LPThe shell flag \fB\(mie\fPcauses the shell to terminateif any error is detected..DS1 hangup2 interrupt3* quit4* illegal instruction5* trace trap6* IOT instruction7* EMT instruction8* floating point exception9 kill (cannot be caught or ignored)10* bus error11* segmentation violation12* bad argument to system call13 write on a pipe with no one to read it14 alarm clock15 software termination (from \fIkill\fP (1)).DE.ft B.ceFigure 3. UNIX signals.ftThose signals marked with an asteriskproduce a core dumpif not caught.However,the shell itself ignores quit which is the onlyexternal signal that can cause a dump.The signals in this list of potential interestto shell programs are 1, 2, 3, 14 and 15..SH3.6\ Fault\ handling.LPShell procedures normally terminatewhen an interrupt is received from theterminal.The \fItrap\fP command is usedif some cleaning up is required, suchas removing temporary files.For example,.DS trap\ \'rm\ /tmp/ps$$; exit\'\ 2.DEsets a trap for signal 2 (terminalinterrupt), and if this signal is receivedwill execute the commands.DS rm /tmp/ps$$; exit.DE\fIexit\fP isanother built-in commandthat terminates execution of a shell procedure.The \fIexit\fP is required; otherwise,after the trap has been taken,the shell will resume executingthe procedureat the place where it was interrupted..LPUNIX signals can be handled in one of three ways.They can be ignored, in which casethe signal is never sent to the process.They can be caught, in which case the processmust decide what action to take when thesignal is received.Lastly, they can be left to causetermination of the process withoutit having to take any further action.If a signal is being ignoredon entry to the shell procedure, for example,by invoking it in the background (see 3.7) then \fItrap\fPcommands (and the signal) are ignored..LPThe use of \fItrap\fP is illustratedby this modified version of the \fItouch\fPcommand (Figure 4).The cleanup action is to remove the file \fBjunk$$\fR\|..DS flag= trap\ \'rm\ \(mif\ junk$$;\ exit\'\ 1 2 3 15 for i do\ case\ $i\ in \*(DC\(mic) flag=N ;; \*(DC\*(ST) if\ test\ \(mif\ $i \*(DC then ln\ $i\ junk$$;\ rm\ junk$$ \*(DC elif\ test\ $flag \*(DC then echo\ file\ \\\\\'$i\\\\\'\ does\ not\ exist \*(DC else >$i \*(DC fi \*(DOesac done.DE.sp.ft B.ceFigure 4. The touch command.ft.spThe \fItrap\fP commandappears before the creationof the temporary file;otherwise it would bepossible for the processto die without removingthe file..LPSince there is no signal 0 in UNIXit is used by the shell to indicate thecommands to be executed on exit from theshell procedure..LPA procedure may, itself, elect toignore signals by specifying the nullstring as the argument to trap.The following fragment is taken from the\fInohup\fP command..DS trap \'\' 1 2 3 15.DEwhich causes \fIhangup, interrupt, quit \fRand\fI kill\fRto be ignored both by theprocedure and by invoked commands..LPTraps may be reset by saying.DS trap 2 3.DEwhich resets the traps for signals 2 and 3 to their default values.A list of the current values of traps may be obtainedby writing.DS trap.DE.LPThe procedure \fIscan\fP (Figure 5) is an exampleof the use of \fItrap\fP where there is no exitin the trap command.\fIscan\fP takes each directoryin the current directory, promptswith its name, and then executescommands typed at the terminaluntil an end of file or an interrupt is received.Interrupts are ignored while executingthe requested commands but causetermination when \fIscan\fP iswaiting for input..DS d=\`pwd\` for\ i\ in\ \*(ST do\ if\ test\ \(mid\ $d/$i \*(DOthen\ cd\ $d/$i \*(DO\*(THwhile\ echo\ "$i:" \*(DO\*(TH\*(WHtrap\ exit\ 2 \*(DO\*(TH\*(WHread\ x \*(DO\*(THdo\ trap\ :\ 2;\ eval\ $x;\ done \*(DOfi done.DE.sp.ft B.ceFigure 5. The scan command.ft.sp\fIread x\fR is a built-in command that reads one line from thestandard inputand places the result in the variable \fBx\|.\fPIt returns a non-zero exit status if eitheran end-of-file is read or an interruptis received..SH3.7\ Command\ execution.LPTo run a command (other than a built-in) the shell first createsa new process using the system call \fIfork.\fPThe execution environment for the commandincludes input, output and the states of signals, andis established in the child processbefore the command is executed.The built-in command \fIexec\fPis used in the rare cases when no forkis requiredand simply replaces the shell with a new command.For example, a simple version of the \fInohup\fPcommand looks like.DS trap \\'\\' 1 2 3 15 exec $\*(ST.DEThe \fItrap\fP turns off the signals specifiedso that they are ignored by subsequently created commandsand \fIexec\fP replaces the shell by the commandspecified..LPMost forms of input output redirection have already beendescribed.In the following \fIword\fP is only subjectto parameter and command substitution.No file name generation or blank interpretationtakes place so that, for example,.DS echo \*(ZZ >\*(ST.c.DEwill write its output into a file whose name is \fB\*(ST.c\|.\fPInput output specifications are evaluated left to rightas they appear in the command..IP >\ \fIword\fP 12The standard output (file descriptor 1)is sent to the file \fIword\fP which iscreated if it does not already exist..IP \*(AP\ \fIword\fP 12The standard output is sent to file \fIword.\fPIf the file exists then output is appended(by seeking to the end);otherwise the file is created..IP <\ \fIword\fP 12The standard input (file descriptor 0)is taken from the file \fIword.\fP.IP \*(HE\ \fIword\fP 12The standard input is taken from the linesof shell input that follow up to but notincluding a line consisting only of \fIword.\fPIf \fIword\fP is quoted then no interpretationof the document occurs.If \fIword\fP is not quotedthen parameter and command substitutionoccur and \fB\\\fP is used to quotethe characters \fB\\\fP \fB$\fP \fB\`\fP and the first characterof \fIword.\fPIn the latter case \fB\\newline\fP is ignored (c.f. quoted strings)..IP >&\ \fIdigit\fP 12The file descriptor \fIdigit\fP is duplicated using the systemcall \fIdup\fP (2)and the result is used as the standard output..IP <&\ \fIdigit\fP 12The standard input is duplicated from file descriptor \fIdigit.\fP.IP <&\(mi 12The standard input is closed..IP >&\(mi 12The standard output is closed..LPAny of the above may be preceded by a digit in whichcase the file descriptor created is that specified by the digitinstead of the default 0 or 1.For example,.DS \*(ZZ 2>file.DEruns a command with message output (file descriptor 2)directed to \fIfile.\fP.DS \*(ZZ 2>&1.DEruns a command with its standard output and message outputmerged.(Strictly speaking file descriptor 2 is createdby duplicating file descriptor 1 but the effect is usually tomerge the two streams.).LPThe environment for a command run in the background such as.DS list \*(ST.c \*(VT lpr &.DEis modified in two ways.Firstly, the default standard inputfor such a command is the empty file \fB/dev/null\|.\fRThis prevents two processes (the shell and the command),which are running in parallel, from trying toread the same input.Chaos would ensueif this were not the case.For example,.DS ed file &.DEwould allow both the editor and the shellto read from the same input at the same time..LPThe other modification to the environment of a backgroundcommand is to turn off the QUIT and INTERRUPT signalsso that they are ignored by the command.This allows these signals to be usedat the terminal without causing backgroundcommands to terminate.For this reason the UNIX conventionfor a signal is that if it is set to 1(ignored) then it is never changedeven for a short time.Note that the shell command \fItrap\fPhas no effect for an ignored signal..SH3.8\ Invoking\ the\ shell.LPThe following flags are interpreted by the shellwhen it is invoked.If the first character of argument zero is a minus,then commands are read from the file \fB.profile\|.\fP.IP \fB\(mic\fP\ \fIstring\fP.brIf the \fB\(mic\fP flag is present thencommands are read from \fIstring\|.\fP.IP \fB\(mis\fPIf the \fB\(mis\fP flag is present or if noarguments remainthen commands are read from the standard input.Shell output is written tofile descriptor 2..IP \fB\(mii\fPIf the \fB\(mii\fP flag is present orif the shell input and output are attached to a terminal (as told by \fIgtty\fP)then this shell is \fIinteractive.\fPIn this case TERMINATE is ignored (so that \fBkill 0\fPdoes not kill an interactive shell) and INTERRUPT is caught and ignored(so that \fBwait\fP is interruptable).In all cases QUIT is ignored by the shell..SHAcknowledgements.LPThe design of the shell isbased in part on the original UNIX shell.[unix command language thompson.]and the PWB/UNIX shell,.[pwb shell mashey unix.]somefeatures having been taken from both.Similarities also exist with thecommand interpretersof the Cambridge Multiple Access System.[cambridge multiple access system hartley.]and of CTSS..[ctss.].LPI would like to thank Dennis Ritchieand John Mashey for manydiscussions during the design of the shell.I am also grateful to the members of the Computing Science Research Centerand to Joe Maranzano for theircomments on drafts of this document..SH.[$LIST$.]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -