⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t1

📁 unix v7是最后一个广泛发布的研究型UNIX版本
💻
字号:
.RP.TL An Introduction to the UNIX Shell.AUS. R. Bourne.AI.MH.AB.LPThe.ulshellis a command programming language that provides an interfaceto the.UXoperating system.Its features includecontrol-flow primitives, parameter passing, variables andstring substitution.Constructs such as.ulwhile, if then else, caseand.ulforare available.Two-way communication is possible between the.ulshelland commands.String-valued parameters, typically file names or flags, may bepassed to a command.A return code is set by commands that may be used to determine control-flow,and the standard output from a command may be usedas shell input..LPThe.ulshellcan modify the environmentin which commands run.Input and output can be redirectedto files, and processes that communicate through `pipes'can be invoked.Commands are found bysearching directoriesin the file system in asequence that can be defined by the user.Commands can be read either from the terminal or from a file,which allows command procedures to bestored for later use..AE.ds ST \v'.3m'\s+2*\s0\v'-.3m'.SH1.0\ Introduction.LPThe shell is both a command languageand a programming languagethat provides an interface to the UNIXoperating system.This memorandum describes, withexamples, the UNIX shell.The first section covers most of theeveryday requirementsof terminal users.Some familiarity with UNIXis an advantage when reading this section;see, for example,"UNIX for beginners"..[unix beginn kernigh 1978.]Section 2 describes those featuresof the shell primarily intendedfor use within shell procedures.These include the control-flowprimitives and string-valued variablesprovided by the shell.A knowledge of a programming languagewould be a help when reading this section.The last section describes the moreadvanced features of the shell.References of the form "see \fIpipe\fP (2)"are to a section of the UNIX manual..[seventh 1978 ritchie thompson.].SH1.1\ Simple\ commands.LPSimple commands consist of one or more wordsseparated by blanks.The first word is the name of the commandto be executed; any remaining wordsare passed as arguments to the command.For example,.DS	who.DEis a command that prints the namesof users logged in.The command.DS	ls \(mil.DEprints a list of files in the currentdirectory.The argument \fI\(mil\fP tells \fIls\fPto print status information, size andthe creation date for each file..SH1.2\ Background\ commands.LPTo execute a command the shell normallycreates a new \fIprocess\fPand waits for it to finish.A command may be run without waitingfor it to finish.For example,.DS	cc pgm.c &.DEcalls the C compiler to compilethe file \fIpgm.c\|.\fPThe trailing \fB&\fP is an operator that instructs the shellnot to wait for the command to finish.To help keep track of such a processthe shell reports its processnumber following its creation.A list of currently active processes may be obtainedusing the \fIps\fP command..SH1.3\ Input\ output\ redirection.LPMost commands produce output on the standard outputthat is initially connected to the terminal.This output may be sent to a fileby writing, for example,.DS	ls \(mil >file.DEThe notation \fI>file\fPis interpreted by the shell and is not passedas an argument to \fIls.\fPIf \fIfile\fP does not exist then theshell creates it;otherwise the original contents of\fIfile\fP are replaced with the outputfrom \fIls.\fPOutput may be appended to a fileusing the notation.DS	ls \(mil \*(APfile.DEIn this case \fIfile\fP is also created if it does not alreadyexist..LPThe standard input of a command may be takenfrom a file instead of the terminal bywriting, for example,.DS	wc <file.DEThe command \fIwc\fP reads its standard input(in this case redirected from \fIfile\fP)and prints the number of characters, words andlines found.If only the number of lines is requiredthen.DS	wc \(mil <file.DEcould be used..SH1.4\ Pipelines\ and\ filters.LPThe standard output of one command may beconnected to the standard input of anotherby writingthe `pipe' operator,indicated by \*(VT,as in,.DS	ls \(mil \*(VT wc.DETwo commands connected in this way constitutea \fIpipeline\fP andthe overall effect is the same as.DS	ls \(mil >file; wc <file.DEexcept that no \fIfile\fP is used.Instead the two processes are connectedby a pipe (see \fIpipe\fP (2)) and arerun in parallel.Pipes are unidirectional andsynchronization is achieved byhalting \fIwc\fP when there isnothing to read and halting \fIls\fPwhen the pipe is full..LPA \fIfilter\fP is a commandthat reads its standard input,transforms it in some way,and prints the result as output.One such filter, \fIgrep,\fPselects from its input those linesthat contain some specified string.For example,.DS	ls \*(VT grep old.DEprints those lines, if any, of the outputfrom \fIls\fP that containthe string \fIold.\fPAnother useful filter is \fIsort\fP.For example,.DS	who \*(VT sort.DEwill print an alphabetically sorted listof logged in users..LPA pipeline may consist of more than two commands,for example,.DS	ls \*(VT grep old \*(VT wc \(mil.DEprints the number of file namesin the current directory containingthe string \fIold.\fP.SH1.5\ File\ name\ generation.LPMany commands accept argumentswhich are file names.For example,.DS	ls \(mil main.c.DEprints information relating to the file \fImain.c\fP\|..LPThe shell provides a mechanismfor generating a list of file namesthat match a pattern.For example,.DS	ls \(mil \*(ST.c.DEgenerates, as arguments to \fIls,\fPall file names in the current directory that end in \fI.c\|.\fPThe character \*(ST is a pattern that will match any stringincluding the null string.In general \fIpatterns\fP are specifiedas follows..RS.IP \fB\*(ST\fR 8Matches any string of charactersincluding the null string..IP \fB?\fR 8Matches any single character..IP \fB[\*(ZZ]\fR 8Matches any one of the charactersenclosed.A pair of characters separated by a minus willmatch any character lexically betweenthe pair..RE.LPFor example,.DS	[a\(miz]\*(ST.DEmatches all names in the current directorybeginning withone of the letters \fIa\fP through \fIz.\fP.DS	/usr/fred/test/?.DEmatches all names in the directory\fB/usr/fred/test\fP that consist of a single character.If no file name is found that matchesthe pattern then the pattern is passed,unchanged, as an argument..LPThis mechanism is useful both to save typingand to select names according to some pattern.It may also be used to find files.For example,.DS	echo /usr/fred/\*(ST/core.DEfinds and prints the names of all \fIcore\fP files in sub-directoriesof \fB/usr/fred\|.\fP(\fIecho\fP is a standard UNIX command that printsits arguments, separated by blanks.)This last feature can be expensive,requiring a scan of allsub-directories of \fB/usr/fred\|.\fP.LPThere is one exception to the generalrules given for patterns.The character `\fB.\fP'at the start of a file name must be explicitlymatched..DS	echo \*(ST.DEwill therefore echo all file names in the currentdirectory not beginningwith `\fB.\fP'\|..DS	echo \fB.\fP\*(ST.DEwill echo all those file names that begin with `\fB.\fP'\|.This avoids inadvertent matchingof the names `\fB.\fP' and `\fB..\fP'which mean `the current directory'and `the parent directory'respectively.(Notice that \fIls\fP suppressesinformation for the files `\fB.\fP' and `\fB..\fP'\|.).SH1.6\ Quoting.LPCharacters that have a special meaningto the shell, such as \fB< > \*(ST ? \*(VT &\|,\fRare called metacharacters.A complete list of metacharacters is givenin appendix B.Any character preceded by a \fB\\\fR is \fIquoted\fPand loses its special meaning, if any.The \fB\\\fP is elided so that.DS	echo \\\\?.DEwill echo a single \fB?\|,\fPand.DS	echo \\\\\\\\.DEwill echo a single \fB\\\|.\fRTo allow long strings to be continued overmore than one linethe sequence \fB\\newline\fPis ignored..LP\fB\\\fP is convenient for quotingsingle characters.When more than one character needsquoting the above mechanism is clumsy anderror prone.A string of characters may be quotedby enclosing the string between single quotes.For example,.DS	echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx.DEwill echo.DS	xx\*(ST\*(ST\*(ST\*(STxx.DEThe quoted string may not containa single quotebut may contain newlines, which are preserved.This quoting mechanism is the mostsimple and is recommendedfor casual use..LPA third quoting mechanism using double quotesis also availablethat prevents interpretation of some but not allmetacharacters.Discussion of thedetails is deferredto section 3.4\|..SH1.7\ Prompting.LPWhen the shell is used from a terminal it willissue a prompt before reading a command.By default this prompt is `\fB$\ \fR'\|.It may be changed by saying,for example,.DS	\s-1PS1\s0=yesdear.DEthat sets the prompt to be the string \fIyesdear\|.\fPIf a newline is typed and further input is neededthen the shell will issue the prompt `\fB>\ \fR'\|.Sometimes this can be caused by mistypinga quote mark.If it is unexpected then an interrupt (\s-1DEL\s0)will return the shell to read another command.This prompt may be changed by saying, for example,.DS	\s-1PS2\s0=more.DE.SH1.8\ The\ shell\ and\ login.LPFollowing \fIlogin\fP (1)the shell is called to read and executecommands typed at the terminal.If the user's login directorycontains the file \fB.profile\fPthen it is assumed to contain commandsand is read by the shell before readingany commands from the terminal..SH1.9\ Summary.sp.RS.IP \(bu\fBls\fP.brPrint the names of files in the current directory..IP \(bu\fBls >file\fP.brPut the output from \fIls\fP into \fIfile.\fP.IP \(bu\fBls \*(VT wc \(mil\fR.brPrint the number of files in the current directory..IP \(bu\fBls \*(VT grep old\fR.brPrint those file names containing the string \fIold.\fP.IP \(bu\fBls \*(VT grep old \*(VT wc \(mil\fR.brPrint the number of files whose name contains the string \fIold.\fP.IP \(bu\fBcc pgm.c &\fR.brRun \fIcc\fP in the background..RE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -