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

📄 csh.3

📁 早期freebsd实现
💻 3
📖 第 1 页 / 共 2 页
字号:
end.DE.PPThis script makes use of the.I foreachcommand, which causes the shell to execute the commands between the.I foreachand the matching.I endfor each of the values given between `(' and `)' with the namedvariable, in this case `i' set to successive values in the list.Within this loop we may use the command.I breakto stop executing the loop and.I continueto prematurely terminate one iterationand begin the next.After the.I foreachloop the iteration variable(\fIi\fR in this case)has the value at the last iteration..PPWe set the variable.I noglobhere to prevent filename expansion of the members of.I argv.This is a good idea, in general, if the arguments to a shell scriptare filenames which have already been expanded or if the argumentsmay contain filename expansion metacharacters.It is also possible to quote each use of a `$' variable expansion,but this is harder and less reliable..PPThe other control construct used here is a statement of the form.DS\fBif\fR ( expression ) \fBthen\fR	command	...\fBendif\fR.DEThe placement of the keywords here is.B notflexible due to the current implementation of the shell.\(dg.FS\(dgThe following two formats are not currently acceptable to the shell:.sp.in +5.nf\fBif\fR ( expression )		# \fBWon't work!\fR\fBthen\fR	command	...\fBendif\fR.fi.in -5.spand.sp.in +5.nf\fBif\fR ( expression ) \fBthen\fR command \fBendif\fR		# \fBWon't work\fR.in -5.fi.FE.PPThe shell does have another form of the if statement of the form.DS\fBif\fR ( expression ) \fBcommand\fR.DEwhich can be written.DS\fBif\fR ( expression ) \e	command.DEHere we have escaped the newline for the sake of appearance.The command must not involve `\||\|', `&' or `;'and must not be another control command.The second form requires the final `\e' to.B immediatelyprecede the end-of-line..PPThe more general.I ifstatements above also admit a sequence of.I else\-ifpairs followed by a single.I elseand an.I endif,e.g.:.DS\fBif\fR ( expression ) \fBthen\fR	commands\fBelse\fR \fBif\fR (expression ) \fBthen\fR	commands\&...\fBelse\fR	commands\fBendif\fR.DE.PPAnother important mechanism used in shell scripts is the `:' modifier.We can use the modifier `:r' here to extract a root of a filename or`:e' to extract the.I extension.Thus if the variable.I ihas the value`/mnt/foo.bar'then.sp.in +5.nf% echo $i $i:r $i:e/mnt/foo.bar /mnt/foo bar%.sp.in -5.fishows how the `:r' modifier strips off the trailing `.bar' and thethe `:e' modifier leaves only the `bar'.Other modifiers will take off the last component of a pathname leavingthe head `:h' or all but the last component of a pathname leaving thetail `:t'.These modifiers are fully described in the.I cshmanual pages in the User's Reference Manual.It is also possible to use the.I "command substitution"mechanism described in the next major section to perform modificationson strings to then reenter the shell's environment.Since each usage of this mechanism involves the creation of a new process,it is much more expensive to use than the `:' modification mechanism.\(dd.FS\(dd It is also important to note thatthe current implementation of the shell limits the number of `:' modifierson a `$' substitution to 1.Thus.sp.nf.in +5% echo $i $i:h:t/a/b/c /a/b:t%.in -5.fi.spdoes not do what one would expect..FEFinally, we note that the character `#' lexically introduces a shellcomment in shell scripts (but not from the terminal).All subsequent characters on the input line after a `#' are discardedby the shell.This character can be quoted using `\'' or `\e' to place it inan argument word..NH 2Other control structures.PPThe shell also has control structures.I whileand.I switchsimilar to those of C.These take the forms.DS\fBwhile\fR ( expression )	commands\fBend\fR.DEand.DS\fBswitch\fR ( word )\fBcase\fR str1:	commands	\fBbreaksw\fR\& ...\fBcase\fR strn:	commands	\fBbreaksw\fR\fBdefault:\fR	commands	\fBbreaksw\fR\fBendsw\fR.DEFor details see the manual section for.I csh.C programmers should note that we use.I breakswto exit from a.I switchwhile.I breakexits a.I whileor.I foreachloop.A common mistake to make in.I cshscripts is to use.I breakrather than.I breakswin switches..PPFinally,.I cshallows a.I gotostatement, with labels looking like they do in C, i.e.:.DSloop:	commands	\fBgoto\fR loop.DE.NH 2Supplying input to commands.PPCommands run from shell scripts receive by default the standardinput of the shell which is running the script.This is different from previous shells runningunder \s-2UNIX\s0.  It allows shell scripts to fully participatein pipelines, but mandates extra notation for commands which are to takeinline data..PPThus we need a metanotation for supplying inline data to commands inshell scripts.As an example, consider this script which runs the editor todelete leading blanks from the lines in each argument file:.DS% cat deblank# deblank \-\- remove leading blanksforeach i ($argv)ed \- $i << \'EOF\'1,$s/^[ ]*//wq\&\'EOF\'end%.DEThe notation `<< \'EOF\''means that the standard input for the.I edcommand is to come from the text in the shell script fileup to the next line consisting of exactly `\'EOF\''.The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,causes the shell to not perform variable substitution on theintervening lines.In general, if any part of the word following the `<<' which theshell uses to terminate the text to be given to the command is quotedthen these substitutions will not be performed.In this case since we used the form `1,$' in our editor scriptwe needed to insure that this `$' was not variable substituted.We could also have insured this by preceding the `$' here with a `\e',i.e.:.DS1,\e$s/^[ ]*//.DEbut quoting the `EOF' terminator is a more reliable way of achieving thesame thing..NH 2Catching interrupts.PPIf our shell script creates temporary files, we may wish to catchinterruptions of the shell script so that we can clean upthese files.We can then do.DSonintr label.DEwhere.I labelis a label in our program.If an interrupt is received the shell will do a`goto label'and we can remove the temporary files and then do an.I exitcommand (which is built in to the shell)to exit from the shell script.If we wish to exit with a non-zero status we can do.DSexit(1).DEe.g. to exit with status `1'..NH 2What else?.PPThere are other features of the shell useful to writers of shellprocedures.The.I verboseand.I echooptions and the related.I \-vand.I \-xcommand line options can be used to help trace the actions of the shell.The.I \-noption causes the shell only to read commands and not to executethem and may sometimes be of use..PPOne other thing to note is that.I cshwill not execute shell scripts which do not begin with thecharacter `#', that is shell scripts that do not begin with a comment.Similarly, the `/bin/sh' on your system may well defer to `csh'to interpret shell scripts which begin with `#'.This allows shell scripts for both shells to live in harmony..PPThere is also another quotation mechanism using `"' which allowsonly some of the expansion mechanisms we have so far discussed to occuron the quoted string and serves to make this string into a single wordas `\'' does..bp

⌨️ 快捷键说明

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