W. Joy - An Introduction to the C shell (794270), страница 7
Текст из файла (страница 7)
In the above example the ‘ed’ job was still in ‘/mnt/bill/project’ even though theshell had changed to ‘/mnt/bill’. A similar warning is given when such a foreground job terminates or issuspended (using the STOP signal) since the return to the shell again implies a change of working directory.----USD:4-22An Introduction to the C shell% fged prog.c (wd: ˜/myproject). .
. after some editingq(wd now: ˜)%These messages are sometimes confusing if you use programs that change their own working directories,since the shell only remembers which directory a job is started in, and assumes it stays there. The ‘−l’option of jobs will type the working directory of suspended or background jobs when it is different fromthe current working directory of the shell.2.8.
Useful built-in commandsWe now give a few of the useful built-in commands of the shell describing how they are used.The alias command described above is used to assign new aliases and to show the existing aliases.With no arguments it prints the current aliases. It may also be given only one argument such asalias lsto show the current alias for, e.g., ‘ls’.The echo command prints its arguments. It is often used in shell scripts or as an interactive command to see what filename expansions will produce.The history command will show the contents of the history list.
The numbers given with the historyevents can be used to reference previous events which are difficult to reference using the contextual mechanisms introduced above. There is also a shell variable called prompt. By placing a ‘!’ character in its valuethe shell will there substitute the number of the current command in the history list. You can use this number to refer to this command in a history substitution.
Thus you couldset prompt=´\! % ´Note that the ‘!’ character had to be escaped here even within ‘´’ characters.The limit command is used to restrict use of resources. With no arguments it prints the current limitations:cputimefilesizedatasizestacksizecoredumpsizeunlimitedunlimited5616 kbytes512 kbytesunlimitedLimits can be set, e.g.:limit coredumpsize 128kMost reasonable units abbreviations will work; see the csh manual page for more details.The logout command can be used to terminate a login shell which has ignoreeof set.The rehash command causes the shell to recompute a table of where commands are located.
This isnecessary if you add a command to a directory in the current shell’s search path and wish the shell to find it,since otherwise the hashing algorithm may tell the shell that the command wasn’t in that directory when thehash table was computed.The repeat command can be used to repeat a command several times. Thus to make 5 copies of thefile one in the file five you could dorepeat 5 cat one >> fiveThe setenv command can be used to set variables in the environment. Thus----An Introduction to the C shellUSD:4-23setenv TERM adm3awill set the value of the environment variable TERM to ‘adm3a’.
A user program printenv exists which willprint out the environment. It might then show:% printenvHOME=/usr/billSHELL=/bin/cshPATH=:/usr/ucb:/bin:/usr/bin:/usr/localTERM=adm3aUSER=bill%The source command can be used to force the current shell to read commands from a file. Thussource .cshrccan be used after editing in a change to the .cshrc file which you wish to take effect right away.The time command can be used to cause a command to be timed no matter how muchtakes. ThusCPUtime it% time cp /etc/rc /usr/bill/rc0.0u 0.1s 0:01 8% 2+1k 3+2io 1pf+0w% time wc /etc/rc /usr/bill/rc52 178 1347 /etc/rc52 178 1347 /usr/bill/rc104 356 2694 total0.1u 0.1s 0:00 13% 3+3k 5+3io 7pf+0w%indicates that the cp command used a negligible amount of user time (u) and about 1/10th of a system time(s); the elapsed time was 1 second (0:01), there was an average memory usage of 2k bytes of program spaceand 1k bytes of data space over the cpu time involved (2+1k); the program did three disk reads and two diskwrites (3+2io), and took one page fault and was not swapped (1pf+0w).
The word count command wc onthe other hand used 0.1 seconds of user time and 0.1 seconds of system time in less than a second ofelapsed time. The percentage ‘13%’ indicates that over the period when it was active the command ‘wc’used an average of 13 percent of the available CPU cycles of the machine.The unalias and unset commands can be used to remove aliases and variable definitions from theshell, and unsetenv removes variables from the environment.2.9. What else?This concludes the basic discussion of the shell for terminal users.
There are more features of theshell to be discussed here, and all features of the shell are discussed in its manual pages. One useful featurewhich is discussed later is the foreach built-in command which can be used to run the same commandsequence with a number of different arguments.If you intend to use UNIX a lot you you should look through the rest of this document and the cshmanual pages (section1) to become familiar with the other facilities which are available to you.----USD:4-24An Introduction to the C shell3. Shell control structures and command scripts3.1. IntroductionIt is possible to place commands in files and to cause shells to be invoked to read and execute commands from these files, which are called shell scripts.
We here detail those features of the shell useful tothe writers of such scripts.3.2. MakeIt is important to first note what shell scripts are not useful for. There is a program called makewhich is very useful for maintaining a group of related files or performing sets of operations on relatedfiles. For instance a large program consisting of one or more files can have its dependencies described in amakefile which contains definitions of the commands used to create these different files when changesoccur. Definitions of the means for printing listings, cleaning up the directory in which the files reside, andinstalling the resultant programs are easily, and most appropriately placed in this makefile.
This format issuperior and preferable to maintaining a group of shell procedures to maintain these files.Similarly when working on a document a makefile may be created which defines how different versions of the document are to be created and which options of nroff or troff are appropriate.3.3. Invocation and the argv variableA csh command script may be interpreted by saying% csh script ...where script is the name of the file containing a group of csh commands and ‘...’ is replaced by a sequenceof arguments.
The shell places these arguments in the variable argv and then begins to read commandsfrom the script. These parameters are then available through the same mechanisms which are used to reference any other shell variables.If you make the file ‘script’ executable by doingchmod 755 scriptand 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’ when you typescriptIf the file does not begin with a ‘#’ then the standard shell ‘/bin/sh’ will be used to execute it. This allowsyou to convert your older shell scripts to use csh at your convenience.3.4.
Variable substitutionAfter each input line is broken into words and history substitutions are done on it, the input line isparsed into distinct commands. Before each command is executed a mechanism know as variable substitution is done on these words. Keyed by the character ‘$’ this substitution replaces the names of variables bytheir values. Thusecho $argvwhen placed in a command script would cause the current value of the variable argv to be echoed to theoutput of the shell script.
It is an error for argv to be unset at this point.A number of notations are provided for accessing components and attributes of variables. The notation$?nameexpands to ‘1’ if name is set or to ‘0’ if name is not set. It is the fundamental mechanism used for checkingwhether particular variables have been assigned values. All other forms of reference to undefined variablescause errors.----An Introduction to the C shellUSD:4-25The notation$#nameexpands to the number of elements in the variable name. Thus% set argv=(a b c)% echo $?argv1% echo $#argv3% unset argv% echo $?argv0% echo $argvUndefined variable: argv.%It is also possible to access the components of a variable which has several values. Thus$argv[1]gives the first component of argv or in the example above ‘a’.
Similarly$argv[$#argv]would give ‘c’, and$argv[1−2]would give ‘a b’. Other notations useful in shell scripts are$nwhere n is an integer as a shorthand for$argv[n ]the n th parameter and$*which is a shorthand for$argvThe form$$expands 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$<is quite special and is replaced by the next line of input read from the shell’s standard input (not the script itis reading).