📄 csh.3
字号:
.\" Copyright (c) 1980, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)csh.3 8.1 (Berkeley) 6/8/93.\".nr H1 2.NHShell control structures and command scripts.NH 2Introduction.PPIt is possible to place commands in files and to cause shells to beinvoked to read and execute commands from these files,which are called.I "shell scripts."We here detail those features of the shell useful to the writers of suchscripts..NH 2Make.PPIt is important to first note what shell scripts are.I notuseful for.There is a program called.I makewhich is very useful for maintaining a group of related filesor performing sets of operations on related files.For instance a large program consisting of one or more filescan have its dependencies described in a.I makefilewhich contains definitions of the commands used to create thesedifferent files when changes occur.Definitions of the means for printing listings, cleaning up the directoryin which the files reside, and installing the resultant programsare easily, and most appropriately placed in this.I makefile.This format is superior and preferable to maintaining a group of shellprocedures to maintain these files..PPSimilarly when working on a document a.I makefilemay be created which defines how different versions of the documentare to be created and which options of.I nroffor.I troffare appropriate..NH 2Invocation and the argv variable.PPA.I cshcommand script may be interpreted by saying.DS% csh script ....DEwhere.I scriptis the name of the file containing a group of.I cshcommands and`\&...' is replaced by a sequence of arguments.The shell places these arguments in the variable.I argvand then begins to read commands from the script.These parameters are then available through the same mechanismswhich are used to reference any other shell variables..PPIf you make the file`script'executable by doing.DSchmod 755 script.DEand place a shell comment at the beginning of the shell script(i.e. begin the file with a `#' character)then a `/bin/csh' will automatically be invoked to execute `script' whenyou type.DSscript.DEIf the file does not begin with a `#' then the standard shell`/bin/sh' will be used to execute it.This allows you to convert your older shell scripts to use.I cshat your convenience..NH 2Variable substitution.PPAfter each input line is broken into words and history substitutionsare done on it, the input line is parsed into distinct commands.Before each command is executed a mechanism know as.I "variable substitution"is done on these words.Keyed by the character `$' this substitution replaces the namesof variables by their values.Thus.DSecho $argv.DEwhen placed in a command script would cause the current value of thevariable.I argvto be echoed to the output of the shell script.It is an error for.I argvto be unset at this point..PPA number of notations are provided for accessing components and attributesof variables.The notation.DS$?name.DEexpands to `1' if name is.I setor to `0'if name is not.I set.It is the fundamental mechanism used for checking whether particularvariables have been assigned values.All other forms of reference to undefined variables cause errors..PPThe notation.DS$#name.DEexpands to the number of elements in the variable.I name.Thus.DS% set argv=(a b c)% echo $?argv1% echo $#argv3% unset argv% echo $?argv0% echo $argvUndefined variable: argv.%.DE.PPIt is also possible to access the components of a variablewhich has several values.Thus.DS$argv[1].DEgives the first component of.I argvor in the example above `a'.Similarly.DS$argv[$#argv].DEwould give `c',and.DS$argv[1\-2].DEwould give `a b'. Other notations useful in shell scripts are.DS$\fIn\fR.DEwhere.I nis an integer as a shorthand for.DS$argv[\fIn\fR\|].DEthe.I n\|thparameter and.DS$*.DEwhich is a shorthand for.DS$argv.DEThe form.DS$$.DEexpands to the process number of the current shell.Since this process number is unique in the system it canbe used in generation of unique temporary file names.The form.DS$<.DEis quite special and is replaced by the next line of input read fromthe shell's standard input (not the script it is reading). This isuseful for writing shell scripts that are interactive, readingcommands from the terminal, or even writing a shell script thatacts as a filter, reading lines from its input file. Thus the sequence.DSecho 'yes or no?\ec'set a=($<).DEwould write out the prompt `yes or no?' without a newline and thenread the answer into the variable `a'. In this case `$#a' would be`0' if either a blank line or end-of-file (^D) was typed..PPOne minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'should be noted here.The form`$argv[\fIn\fR\|]'will yield an error if.I nis not in the range`1\-$#argv'while `$n'will never yield an out of range subscript error.This is for compatibility with the way older shells handled parameters..PPAnother important point is that it is never an error to give a subrangeof the form `n\-'; if there are less than.I ncomponents of the given variable then no words are substituted.A range of the form `m\-n' likewise returns an empty vector without givingan error when \fIm\fR exceeds the number of elements of the given variable,provided the subscript \fIn\fR is in range..NH 2Expressions.PPIn order for interesting shell scripts to be constructed itmust be possible to evaluate expressions in the shell based on thevalues of variables.In fact, all the arithmetic operations of the language C are availablein the shellwith the same precedence that they have in C.In particular, the operations `==' and `!=' compare stringsand the operators `&&' and `|\|\||' implement the boolean and/or operations.The special operators `=~' and `!~' are similar to `==' and `!=' exceptthat the string on the right side can have pattern matching characters(like *, ? or []) and the test is whether the string on the left matchesthe pattern on the right..PPThe shell also allows file enquiries of the form.DS\-? filename.DEwhere `?' is replace by a number of single characters.For instance the expression primitive.DS\-e filename.DEtell whether the file`filename'exists.Other primitives test for read, write and execute access to the file,whether it is a directory, or has non-zero length..PPIt is possible to test whether a command terminates normally,by a primitive of theform `{ command }' which returns true, i.e. `1' if the commandsucceeds exiting normally with exit status 0, or `0' if the commandterminates abnormally or with exit status non-zero.If more detailed information about the execution status of a commandis required, it can be executed and the variable `$status' examinedin the next command.Since `$status' is set by every command, it is very transient.It can be saved if it is inconvenient to use it only in the singleimmediately following command..PPFor a full list of expression components available see the manualsection for the shell..NH 2Sample shell script.PPA sample shell script which makes use of the expression mechanismof the shell and some of its control structure follows:.DS% cat copyc## Copyc copies those C programs in the specified list# to the directory ~/backup if they differ from the files# already in ~/backup#set noglobforeach i ($argv) if ($i !~ *.c) continue # not a .c file so do nothing if (! \-r ~/backup/$i:t) then echo $i:t not in backup... not cp\e\'ed continue endif cmp \-s $i ~/backup/$i:t # to set $status if ($status != 0) then echo new backup of $i cp $i ~/backup/$i:t endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -