W. Joy - An Introduction to the C shell (794270), страница 9
Текст из файла (страница 9)
For instance, there were at one point three shells in use on the Cory UNIXsystem at Cory Hall, ‘/bin/sh’, ‘/bin/nsh’, and ‘/bin/csh’. To count the number of persons using each shellone could have issued the commands% grep −c csh$ /etc/passwd27% grep −c nsh$ /etc/passwd128% grep −c −v sh$ /etc/passwd430%Since these commands are very similar we can use foreach to do this more easily.% foreach i (´sh$´ ´csh$´ ´−v sh$´)? grep −c $i /etc/passwd? end27128430%Note here that the shell prompts for input with ‘? ’ when reading the body of the loop.Very useful with loops are variables which contain lists of filenames or other words.
You can, forexample, do% set a=(`ls`)% echo $acsh.n csh.rm% lscsh.ncsh.rm% echo $#a2%The set command here gave the variable a a list of all the filenames in the current directory as value. Wecan then iterate over these names to perform any chosen function.The output of a command within ‘`’ characters is converted by the shell to a list of words. You canalso place the ‘`’ quoted string within ‘"’ characters to take each (non-empty) line as a component of thevariable; preventing the lines from being split into words at blanks and tabs. A modifier ‘:x’ exists whichcan be used later to expand each component of the variable into another variable splitting it into separatewords at embedded blanks and tabs.4.2.
Braces { ... } in argument expansionAnother form of filename expansion, alluded to before involves the characters ‘{’ and ‘}’. Thesecharacters specify that the contained strings, separated by ‘,’ are to be consecutively substituted into thecontaining characters and the results expanded left to right. ThusA{str1,str2,...strn}Bexpands to----USD:4-32An Introduction to the C shellAstr1B Astr2B ... AstrnBThis expansion occurs before the other filename expansions, and may be applied recursively (i.e.
nested).The results of each expanded string are sorted separately, left to right order being preserved. The resultingfilenames are not required to exist if no other expansion mechanisms are used. This means that this mechanism can be used to generate arguments which are not filenames, but which have common parts.A typical use of this would bemkdir ˜/{hdrs,retrofit,csh}to make subdirectories ‘hdrs’, ‘retrofit’ and ‘csh’ in your home directory.
This mechanism is most usefulwhen the common prefix is longer than in this example, i.e.chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}4.3. Command substitutionA command enclosed in ‘`’ characters is replaced, just before filenames are expanded, by the outputfrom that command. Thus it is possible to doset pwd=`pwd`to save the current directory in the variable pwd or to doex `grep −l TRACE *.c`to run the editor ex supplying as arguments those files whose names end in ‘.c’ which have the string‘TRACE’ in them.*4.4. Other details not covered hereIn particular circumstances it may be necessary to know the exact nature and order of different substitutions performed by the shell. The exact meaning of certain combinations of quotations is also occasionally important.
These are detailed fully in its manual section.The shell has a number of command line option flags mostly of use in writingdebugging shell scripts. See the csh(1) manual section for a list of these options.UNIXprograms, and*Command expansion also occurs in input redirected with ‘<<’ and within ‘"’ quotations. Refer to the shell manual sectionfor full details.----An Introduction to the C shellUSD:4-33Appendix − Special charactersThe following table lists the special characters of csh and the UNIX system, giving for each the section(s) inwhich it is discussed. A number of these characters also have special meaning in expressions.
See the cshmanual section for a complete list.Syntactic metacharacters;|()&2.4 separates commands to be executed sequentially1.5 separates commands in a pipeline2.2,3.6brackets expressions and variable values2.5 follows commands to be executed without waiting for completionFilename metacharacters/?*[]˜{}1.61.61.61.61.64.2separates components of a file’s pathnameexpansion character matching any single characterexpansion character matching any sequence of charactersexpansion sequence matching any single character from a setused at the beginning of a filename to indicate home directoriesused to specify groups of arguments with common partsQuotation metacharacters\´"1.71.74.3prevents meta-meaning of following single characterprevents meta-meaning of a group of characterslike ´, but allows variable and command expansionInput/output metacharacters<>1.51.3indicates redirected inputindicates redirected outputExpansion/substitution metacharacters$!:ˆ`3.42.33.62.34.3indicates variable substitutionindicates history substitutionprecedes substitution modifiersused in special forms of history substitutionindicates command substitutionOther metacharacters#−%1.3,3.6begins scratch file names; indicates shell comments1.2 prefixes option (flag) arguments to commands2.6 prefixes job name specifications----USD:4-34An Introduction to the C shellGlossaryThis glossary lists the most important terms introduced in the introduction to the shell and gives references to sections of the shell document for further information about them.
References of the form ‘pr(1)’ indicate that the command pr is in the UNIX User Reference manual in section 1. You can look at anonline copy of its manual page by doingman 1 prReferences of the form (2.5) indicate that more information can be found in section 2.5 of this manual..Your current directory has the name ‘.’ as well as the name printed by the commandpwd; see also dirs. The current directory ‘.’ is usually the first component of the searchpath contained in the variable path , thus commands which are in ‘.’ are found first (2.2).The character ‘.’ is also used in separating components of filenames (1.6).
The character‘.’ at the beginning of a component of a pathname is treated specially and not matchedby the filename expansion metacharacters ‘?’, ‘*’, and ‘[’ ‘]’ pairs (1.6)...Each directory has a file ‘..’ in it which is a reference to its parent directory. Afterchanging into the directory with chdir , i.e.chdir paperyou can return to the parent directory by doingchdir ..The current directory is printed by pwd (2.7).a.outCompilers which create executable images create them, by default, in the file a.out. forhistorical reasons (2.3).absolute pathnameA pathname which begins with a ‘/’ is absolute since it specifies the path of directoriesfrom the beginning of the entire directory system − called the root directory.
Pathname swhich are not absolute are called relative (see definition of relative pathname ) (1.6).aliasAn alias specifies a shorter or different name for a UNIX command, or a transformationon a command to be performed in the shell. The shell has a command alias whichestablishes aliases and can print their current values. The command unalias is used toremove aliases (2.4).argumentCommands in UNIX receive a list of argument words.
Thus the commandecho a b cconsists of the command name ‘echo’ and three argument words ‘a’, ‘b’ and ‘c’. Theset of arguments after the command name is said to be the argument list of the command (1.1).argvThe list of arguments to a command written in the shell language (a shell script or shellprocedure) is stored in a variable called argv within the shell. This name is taken fromthe conventional name in the C programming language (3.4).backgroundCommands started without waiting for them to complete are called background commands (2.6).baseA filename is sometimes thought of as consisting of a base part, before any ‘.’ character,and an extension − the part after the ‘.’.
See filename and extension (1.6) and basename(1).bgThe bg command causes a suspended job to continue execution in the background(2.6).binA directory containing binaries of programs and shell scripts to be executed is typicallycalled a bin directory. The standard system bin directories are ‘/bin’ containing themost heavily used commands and ‘/usr/bin’ which contains most other user programs.----An Introduction to the C shellUSD:4-35Programs developed at UC Berkeley live in ‘/usr/ucb’, while locally written programslive in ‘/usr/local’. Games are kept in the directory ‘/usr/games’.
You can place binariesin any directory. If you wish to execute them often, the name of the directories should bea component of the variable path .breakBreak is a builtin command used to exit from loops within the control structure of theshell (3.7).breakswThe breaksw builtin command is used to exit from a switch control structure, like abreak exits from loops (3.7).builtinA command executed directly by the shell is called a builtin command. Most commandsin UNIX are not built into the shell, but rather exist as files in bin directories.
These commands are accessible because the directories in which they reside are named in the pathvariable.caseA case command is used as a label in a switch statement in the shell’s control structure,similar to that of the language C. Details are given in the shell documentation ‘csh (1)’(3.7).catThe cat program catenates a list of specified files on the standard output . It is usuallyused to look at the contents of a single file on the terminal, to ‘cat a file’ (1.8, 2.3).cdThe cd command is used to change the working directory .
With no arguments, cdchanges your working directory to be your home directory (2.4, 2.7).chdirThe chdir command is a synonym for cd . Cd is usually used because it is easier totype.chshThe chsh command is used to change the shell which you use on UNIX. By default, youuse an different version of the shell which resides in ‘/bin/sh’. You can change yourshell to ‘/bin/csh’ by doingchsh your-login-name /bin/cshThus I would dochsh bill /bin/cshIt is only necessary to do this once. The next time you log in to UNIX after doing thiscommand, you will be using csh rather than the shell in ‘/bin/sh’ (1.9).cmpCmp is a program which compares files.