W. Joy - An Introduction to the C shell (794270), страница 3
Текст из файла (страница 3)
This mechanism will bedescribed much later, in section 4.2, as it is used less frequently.1.7. QuotationWe have already seen a number of metacharacters used by the shell. These metacharacters pose aproblem in that we cannot use them directly as parts of words. Thus the command----USD:4-8An Introduction to the C shellecho *will not echo the character ‘*’. It will either echo an sorted list of filenames in the current working directory, or print the message ‘No match’ if there are no files in the working directory.The recommended mechanism for placing characters which are neither numbers, digits, ‘/’, ‘.’ or ‘−’in an argument word to a command is to enclose it with single quotation characters ‘´’, i.e.echo ´*´There is one special character ‘!’ which is used by the history mechanism of the shell and which cannot beescaped by placing it within ‘´’ characters.
It and the character ‘´’ itself can be preceded by a single ‘\’ toprevent their special meaning. Thusecho \´\!prints´!These two mechanisms suffice to place any printing character into a word which is an argument to a shellcommand. They can be combined, as inecho \´´*´which prints´*since the first ‘\’ escaped the first ‘´’ and the ‘*’ was enclosed between ‘´’ characters.1.8.
Terminating commandsWhen you are executing a command and the shell is waiting for it to complete there are several waysto force it to stop. For instance if you type the commandcat /etc/passwdthe system will print a copy of a list of all users of the system on your terminal.
This is likely to continuefor several minutes unless you stop it. You can send an INTERRUPT signal to the cat command by typingˆC on your terminal.* Since cat does not take any precautions to avoid or otherwise handle this signal theINTERRUPT will cause it to terminate. The shell notices that cat has terminated and prompts you again with‘% ’. If you hit INTERRUPT again, the shell will just repeat its prompt since it handles INTERRUPT signalsand chooses to continue to execute commands rather than terminating like cat did, which would have theeffect of logging you out.Another way in which many programs terminate is when they get an end-of-file from their standardinput. Thus the mail program in the first example above was terminated when we typed a ˆD which generates an end-of-file from the standard input.
The shell also terminates when it gets an end-of-file printing‘logout’; UNIX then logs you off the system. Since this means that typing too many ˆD’s can accidentallylog us off, the shell has a mechanism for preventing this. This ignoreeof option will be discussed in section2.2.If a command has its standard input redirected from a file, then it will normally terminate when itreaches the end of this file. Thus if we executemail bill < prepared.textthe mail command will terminate without our typing a ˆD. This is because it read to the end-of-file of ourfile ‘prepared.text’ in which we placed a message for ‘bill’ with an editor program.
We could also havedone*On some older Unix systems the DEL or RUBOUT key has the same effect. "stty all" will tell you the INTR key value.----An Introduction to the C shellUSD:4-9cat prepared.text | mail billsince the cat command would then have written the text through the pipe to the standard input of the mailcommand. When the cat command completed it would have terminated, closing down the pipeline and themail command would have received an end-of-file from it and terminated. Using a pipe here is more complicated than redirecting input so we would more likely use the first form. These commands could alsohave been stopped by sending an INTERRUPT.Another possibility for stopping a command is to suspend its execution temporarily, with the possibility of continuing execution later.
This is done by sending a STOP signal via typing a ˆZ. This signalcauses all commands running on the terminal (usually one but more if a pipeline is executing) to becomesuspended. The shell notices that the command(s) have been suspended, types ‘Stopped’ and then promptsfor a new command. The previously executing command has been suspended, but otherwise unaffected bythe STOP signal. Any other commands can be executed while the original command remains suspended.The suspended command can be continued using the fg command with no arguments. The shell will thenretype the command to remind you which command is being continued, and cause the command to resumeexecution.
Unless any input files in use by the suspended command have been changed in the meantime,the suspension has no effect whatsoever on the execution of the command. This feature can be very usefulduring editing, when you need to look at another file before continuing. An example of command suspension follows.% mail haroldSomeone just copied a big file into my directory and its name isˆZStopped% lsfunnyfileprog.cprog.o% jobs[1] + Stoppedmail harold% fgmail haroldfunnyfile. Do you know who did it?EOT%In this example someone was sending a message to Harold and forgot the name of the file he wanted tomention.
The mail command was suspended by typing ˆZ. When the shell noticed that the mail programwas suspended, it typed ‘Stopped’ and prompted for a new command. Then the ls command was typed tofind out the name of the file. The jobs command was run to find out which command was suspended. Atthis time the fg command was typed to continue execution of the mail program. Input to the mail programwas then continued and ended with a ˆD which indicated the end of the message at which time the mail program typed EOT. The jobs command will show which commands are suspended.
The ˆZ should only betyped at the beginning of a line since everything typed on the current line is discarded when a signal is sentfrom the keyboard. This also happens on INTERRUPT, and QUIT signals. More information on suspendingjobs and controlling them is given in section 2.6.If you write or run programs which are not fully debugged then it may be necessary to stop themsomewhat ungracefully.
This can be done by sending them a QUIT signal, sent by typing a ˆ\. This will usually provoke the shell to produce a message like:Quit (Core dumped)indicating that a file ‘core’ has been created containing information about the running program’s state whenit terminated due to the QUIT signal. You can examine this file yourself, or forward information to themaintainer of the program telling him/her where the core file is.----USD:4-10An Introduction to the C shellIf you run background commands (as explained in section 2.6) then these commands will ignoreand QUIT signals at the terminal. To stop them you must use the kill command.
See section 2.6for an example.INTERRUPTIf you want to examine the output of a command without having it move off the screen as the outputof thecat /etc/passwdcommand will, you can use the commandmore /etc/passwdThe more program pauses after each complete screenful and types ‘−−More−−’ at which point you can hita space to get another screenful, a return to get another line, a ‘?’ to get some help on other commands, or a‘q’ to end the more program. You can also use more as a filter, i.e.cat /etc/passwd | moreworks just like the more simple more command above.For stopping output of commands not involving more you can use the ˆS key to stop the typeout.The typeout will resume when you hit ˆQ or any other key, but ˆQ is normally used because it only restartsthe output and does not become input to the program which is running.
This works well on low-speed terminals, but at 9600 baud it is hard to type ˆS and ˆQ fast enough to paginate the output nicely, and a program like more is usually used.An additional possibility is to use the ˆO flush output character; when this character is typed, all output from the current command is thrown away (quickly) until the next input read occurs or until the nextshell prompt. This can be used to allow a command to complete without having to suffer through the output on a slow terminal; ˆO is a toggle, so flushing can be turned off by typing ˆO again while output is beingflushed.1.9. What now?We have so far seen a number of mechanisms of the shell and learned a lot about the way in which itoperates.
The remaining sections will go yet further into the internals of the shell, but you will surely wantto try using the shell before you go any further. To try it you can log in to UNIX and type the followingcommand to the system:chsh myname /bin/cshHere ‘myname’ should be replaced by the name you typed to the system prompt of ‘login:’ to get onto thesystem. Thus I would use ‘chsh bill /bin/csh’. You only have to do this once; it takes effect at nextlogin. You are now ready to try using csh.Before you do the ‘chsh’ command, the shell you are using when you log into the system is ‘/bin/sh’.In fact, much of the above discussion is applicable to ‘/bin/sh’.
The next section will introduce many features particular to csh so you should change your shell to csh before you begin reading it.----An Introduction to the C shellUSD:4-112. Details on the shell for terminal users2.1. Shell startup and terminationWhen you login, the shell is started by the system in your home directory and begins by readingcommands from a file .cshrc in this directory. All shells which you may start during your terminal sessionwill read from this file. We will later see what kinds of commands are usefully placed there. For now weneed not have this file and the shell does not complain about its absence.A login shell , executed after you login to the system, will, after it reads commands from .cshrc, readcommands from a file .login also in your home directory.