W. Joy - An Introduction to the C shell (794270), страница 4
Текст из файла (страница 4)
This file contains commands which you wish todo each time you login to the UNIX system. My .login file looks something like:set ignoreeofset mail=(/usr/spool/mail/bill)echo "${prompt}users" ; usersalias ts \´set noglob ; eval `tset −s −m dialup:c100rv4pna −m plugboard:?hp2621nl *`´;ts; stty intr ˆC kill ˆU crtset time=15 history=10msgs −fif (−e $mail) thenecho "${prompt}mail"mailendifThis file contains several commands to be executed by UNIX each time I login. The first is a set command which is interpreted directly by the shell. It sets the shell variable ignoreeof which causes the shell tonot log me off if I hit ˆD. Rather, I use the logout command to log off of the system. By setting the mailvariable, I ask the shell to watch for incoming mail to me.
Every 5 minutes the shell looks for this file andtells me if more mail has arrived there. An alternative to this is to put the commandbiff yin place of this set; this will cause me to be notified immediately when mail arrives, and to be shown thefirst few lines of the new message.Next I set the shell variable ‘time’ to ‘15’ causing the shell to automatically print out statistics linesfor commands which execute for at least 15 seconds of CPU time. The variable ‘history’ is set to 10 indicating that I want the shell to remember the last 10 commands I type in its history list , (described later).I create an alias ‘‘ts’’ which executes a tset (1) command setting up the modes of the terminal.
Theparameters to tset indicate the kinds of terminal which I usually use when not on a hardwired port. I thenexecute ‘‘ts’’ and also use the stty command to change the interrupt character to ˆC and the line kill character to ˆU.I then run the ‘msgs’ program, which provides me with any system messages which I have not seenbefore; the ‘−f’ option here prevents it from telling me anything if there are no new messages.
Finally, ifmy mailbox file exists, then I run the ‘mail’ program to process my mail.When the ‘mail’ and ‘msgs’ programs finish, the shell will finish processing my .login file and beginreading commands from the terminal, prompting for each with ‘% ’. When I log off (by giving the logoutcommand) the shell will print ‘logout’ and execute commands from the file ‘.logout’ if it exists in my homedirectory.
After that the shell will terminate and UNIX will log me off the system. If the system is not goingdown, I will receive a new login message. In any case, after the ‘logout’ message the shell is committed toterminating and will take no further input from my terminal.----USD:4-12An Introduction to the C shell2.2. Shell variablesThe shell maintains a set of variables. We saw above the variables history and time which had values ‘10’ and ‘15’. In fact, each shell variable has as value an array of zero or more strings.
Shell variablesmay be assigned values by the set command. It has several forms, the most useful of which was givenabove and isset name=valueShell variables may be used to store values which are to be used in commands later through a substitution mechanism. The shell variables most commonly referenced are, however, those which the shell itselfrefers to. By changing the values of these variables one can directly affect the behavior of the shell.One of the most important variables is the variable path. This variable contains a sequence of directory names where the shell searches for commands.
The set command with no arguments shows the valueof all variables currently defined (we usually say set) in the shell. The default value for path will be shownby set to be% setargvcwdhomepathpromptshellstatustermuser%()/usr/bill/usr/bill(. /usr/ucb /bin /usr/bin)%/bin/csh0c100rv4pnabillThis output indicates that the variable path points to the current directory ‘.’ and then ‘/usr/ucb’, ‘/bin’ and‘/usr/bin’. Commands which you may write might be in ‘.’ (usually one of your directories). Commandsdeveloped at Berkeley, live in ‘/usr/ucb’ while commands developed at Bell Laboratories live in ‘/bin’ and‘/usr/bin’.A number of locally developed programs on the system live in the directory ‘/usr/local’. If we wishthat all shells which we invoke to have access to these new programs we can place the commandset path=(.
/usr/ucb /bin /usr/bin /usr/local)in our file .cshrc in our home directory. Try doing this and then logging out and back in and dosetagain to see that the value assigned to path has changed.One thing you should be aware of is that the shell examines each directory which you insert into yourpath and determines which commands are contained there. Except for the current directory ‘.’, which theshell treats specially, this means that if commands are added to a directory in your search path after youhave started the shell, they will not necessarily be found by the shell. If you wish to use a command whichhas been added in this way, you should give the commandrehashto the shell, which will cause it to recompute its internal table of command locations, so that it will find thenewly added command.
Since the shell has to look in the current directory ‘.’ on each command, placing itat the end of the path specification usually works equivalently and reduces overhead.† Another directory that might interest you is /usr/new, which contains many useful user-contributed programs providedwith Berkeley Unix.----An Introduction to the C shellUSD:4-13Other useful built in variables are the variable home which shows your home directory, cwd whichcontains your current working directory, the variable ignoreeof which can be set in your .login file to tellthe shell not to exit when it receives an end-of-file from a terminal (as described above). The variable‘ignoreeof ’ is one of several variables which the shell does not care about the value of, only whether theyare set or unset. Thus to set this variable you simply doset ignoreeofand to unset it dounset ignoreeofThese give the variable ‘ignoreeof’ no value, but none is desired or required.Finally, some other built-in shell variables of use are the variables noclobber and mail.
The metasyntax> filenamewhich redirects the standard output of a command will overwrite and destroy the previous contents of thenamed file. In this way you may accidentally overwrite a file which is valuable. If you would prefer thatthe shell not overwrite files in this way you canset noclobberin your .login file. Then trying to dodate > nowwould cause a diagnostic if ‘now’ existed already. You could typedate >! nowif you really wanted to overwrite the contents of ‘now’. The ‘>!’ is a special metasyntax indicating thatclobbering the file is ok.†2.3.
The shell’s history listThe shell can maintain a history list into which it places the words of previous commands. It is possible to use a notation to reuse commands or words from commands in forming new commands. Thismechanism can be used to repeat previous commands or to correct minor typing mistakes in commands.The following figure gives a sample session involving typical usage of the history mechanism of theshell. In this example we have a very simple C program which has a bug (or two) in it in the file ‘bug.c’,which we ‘cat’ out on our terminal.
We then try to run the C compiler on it, referring to the file again as‘!$’, meaning the last argument to the previous command. Here the ‘!’ is the history mechanism invocationmetacharacter, and the ‘$’ stands for the last argument, by analogy to ‘$’ in the editor which stands for theend of the line. The shell echoed the command, as it would have been typed without use of the historymechanism, and then executed it.
The compilation yielded error diagnostics so we now run the editor onthe file we were trying to compile, fix the bug, and run the C compiler again, this time referring to this command simply as ‘!c’, which repeats the last command which started with the letter ‘c’. If there were othercommands starting with ‘c’ done recently we could have said ‘!cc’ or even ‘!cc:p’ which would haveprinted the last command starting with ‘cc’ without executing it.After this recompilation, we ran the resulting ‘a.out’ file, and then noting that there still was a bug,ran the editor again.
After fixing the program we ran the C compiler again, but tacked onto the commandan extra ‘−o bug’ telling the compiler to place the resultant binary in the file ‘bug’ rather than ‘a.out’. Ingeneral, the history mechanisms may be used anywhere in the formation of new commands and other characters may be placed before and after the substituted commands.†The space between the ‘!’ and the word ‘now’ is critical here, as ‘!now’ would be an invocation of the history mechanism,and have a totally different effect.----USD:4-14% cat bug.cmain(){printf("hello);}% cc !$cc bug.c"bug.c", line 4: newline in string or char constant"bug.c", line 5: syntax error% ed !$ed bug.c294s/);/"&/pprintf("hello");w30q% !ccc bug.c% a.outhello% !eed bug.c304s/lo/lo\\n/pprintf("hello\n");w32q% !c −o bugcc bug.c −o bug% size a.out buga.out: 2784+364+1028 = 4176b = 0x1050bbug: 2784+364+1028 = 4176b = 0x1050b% ls −l !*ls −l a.out bug−rwxr−xr−x 1 bill3932 Dec 19 09:41 a.out−rwxr−xr−x 1 bill3932 Dec 19 09:42 bug% bughello% num bug.c | sppspp: Command not found.% ˆsppˆsspnum bug.c | ssp1 main()3 {4printf("hello\n");5 }% !! | lprnum bug.c | ssp | lpr%An Introduction to the C shell----An Introduction to the C shellUSD:4-15We then ran the ‘size’ command to see how large the binary program images we have created were,and then an ‘ls −l’ command with the same argument list, denoting the argument list ‘*’.
Finally we ran theprogram ‘bug’ to see that its output is indeed correct.To make a numbered listing of the program we ran the ‘num’ command on the file ‘bug.c’. In orderto compress out blank lines in the output of ‘num’ we ran the output through the filter ‘ssp’, but misspelledit as spp. To correct this we used a shell substitute, placing the old text and new text between ‘ˆ’ characters.This is similar to the substitute command in the editor. Finally, we repeated the same command with ‘!!’,but sent its output to the line printer.There are other mechanisms available for repeating commands. The history command prints out anumber of previous commands with numbers by which they can be referenced. There is a way to refer to aprevious command by searching for a string which appeared in it, and there are other, less useful, ways toselect arguments to include in a new command.