W. Joy - An Introduction to the C shell (794270), страница 6
Текст из файла (страница 6)
Thus% du > usageˆZStopped% bg[1] du > usage &%starts ‘du’ in the foreground, stops it before it finishes, then continues it in the background allowing moreforeground commands to be executed. This is especially helpful when a foreground job ends up takinglonger than you expected and you wish you had started it in the backgound in the beginning.All job control commands can take an argument that identifies a particular job. All job name arguments begin with the character ‘%’, since some of the job control commands also accept process numbers(printed by the ps command.) The default job (when no argument is given) is called the current job and isidentified by a ‘+’ in the output of the jobs command, which shows you which jobs you have.
When onlyone job is stopped or running in the background (the usual case) it is always the current job thus no argument is needed. If a job is stopped while running in the foreground it becomes the current job and theexisting current job becomes the previous job − identified by a ‘−’ in the output of jobs. When the currentjob terminates, the previous job becomes the current job.
When given, the argument is either ‘%−’ (indicating the previous job); ‘%#’, where # is the job number; ‘%pref’ where pref is some unique prefix of thecommand name and arguments of one of the jobs; or ‘%?’ followed by some string found in only one of thejobs.The jobs command types the table of jobs, giving the job number, commands and status (‘Stopped’or ‘Running’) of each backgound or suspended job. With the ‘−l’ option the process numbers are alsotyped.% du > usage &[1] 3398% ls −s | sort −n > myfile &[2] 3405% mail billˆZStopped% jobs[1] − Running[2] Running[3] + Stopped% fg %lsls −s | sort −n > myfile% more myfiledu > usagels −s | sort −n > myfilemail billThe fg command runs a suspended or background job in the foreground.
It is used to restart a previously suspended job or change a background job to run in the foreground (allowing signals or input fromthe terminal). In the above example we used fg to change the ‘ls’ job from the background to the foreground since we wanted to wait for it to finish before looking at its output file. The bg command runs asuspended job in the background. It is usually used after stopping the currently running foreground job----An Introduction to the C shellUSD:4-19with the STOP signal. The combination of the STOP signal and the bg command changes a foreground jobinto a background job.
The stop command suspends a background job.The kill command terminates a background or suspended job immediately. In addition to jobs, itmay be given process numbers as arguments, as printed by ps. Thus, in the example above, the running ducommand could have been terminated by the command% kill %1[1] Terminated%du > usageThe notify command (not the variable mentioned earlier) indicates that the termination of a specificjob should be reported at the time it finishes instead of waiting for the next prompt.If a job running in the background tries to read input from the terminal it is automatically stopped.When such a job is then run in the foreground, input can be given to the job.
If desired, the job can be runin the background again until it requests input again. This is illustrated in the following sequence where the‘s’ command in the text editor might take a long time.% ed bigfile1200001,$s/thisword/thatword/ˆZStopped% bg[1] ed bigfile &%. . . some foreground commands[1] Stopped (tty input)ed bigfile% fged bigfilew120000q%So after the ‘s’ command was issued, the ‘ed’ job was stopped with ˆZ and then put in the backgroundusing bg. Some time later when the ‘s’ command was finished, ed tried to read another command and wasstopped because jobs in the backgound cannot read from the terminal. The fg command returned the ‘ed’job to the foreground where it could once again accept commands from the terminal.The commandstty tostopcauses all background jobs run on your terminal to stop when they are about to write output to the terminal.This prevents messages from background jobs from interrupting foreground job output and allows you torun a job in the background without losing terminal output.
It also can be used for interactive programs thatsometimes have long periods without interaction. Thus each time it outputs a prompt for more input it willstop before the prompt. It can then be run in the foreground using fg, more input can be given and, if necessary stopped and returned to the background. This stty command might be a good thing to put in your.login file if you do not like output from background jobs interrupting your work. It also can reduce theneed for redirecting the output of background jobs if the output is not very big:----USD:4-20% stty tostop% wc hugefile &[1] 10387% ed text.
. . some time laterq[1] Stopped (tty output)% fg wcwc hugefile13371 30123 302577% stty −tostopAn Introduction to the C shellwc hugefileThus after some time the ‘wc’ command, which counts the lines, words and characters in a file, had oneline of output. When it tried to write this to the terminal it stopped. By restarting it in the foreground weallowed it to write on the terminal exactly when we were ready to look at its output.
Programs whichattempt to change the mode of the terminal will also block, whether or not tostop is set, when they are notin the foreground, as it would be very unpleasant to have a background job change the state of the terminal.Since the jobs command only prints jobs started in the currently executing shell, it knows nothingabout background jobs started in other login sessions or within shell files. The ps can be used in this caseto find out about background jobs not started in the current shell.2.7.
Working DirectoriesAs mentioned in section 1.6, the shell is always in a particular working directory. The ‘change directory’ command chdir (its short form cd may also be used) changes the working directory of the shell, thatis, changes the directory you are located in.It is useful to make a directory for each project you wish to work on and to place all files related tothat project in that directory. The ‘make directory’ command, mkdir, creates a new directory. The pwd(‘print working directory’) command reports the absolute pathname of the working directory of the shell,that is, the directory you are located in.
Thus in the example below:% pwd/usr/bill% mkdir newpaper% chdir newpaper% pwd/usr/bill/newpaper%the user has created and moved to the directory newpaper. where, for example, he might place a group ofrelated files.No matter where you have moved to in a directory hierarchy, you can return to your ‘home’ logindirectory by doing justcdwith no arguments. The name ‘..’ always means the directory above the current one in the hierarchy, thuscd ..changes the shell’s working directory to the one directly above the current one. The name ‘..’ can be usedin any pathname, thus,cd ../programsmeans change to the directory ‘programs’ contained in the directory above the current one. If you haveseveral directories for different projects under, say, your home directory, this shorthand notation permitsyou to switch easily between them.----An Introduction to the C shellUSD:4-21The shell always remembers the pathname of its current working directory in the variable cwd.
Theshell can also be requested to remember the previous directory when you change to a new working directory. If the ‘push directory’ command pushd is used in place of the cd command, the shell saves the nameof the current working directory on a directory stack before changing to the new one. You can see this listat any time by typing the ‘directories’ command dirs.% pushd newpaper/references˜/newpaper/references ˜% pushd /usr/lib/tmac/usr/lib/tmac ˜/newpaper/references ˜% dirs/usr/lib/tmac ˜/newpaper/references ˜% popd˜/newpaper/references ˜% popd˜%The list is printed in a horizontal line, reading left to right, with a tilde (˜) as shorthand for your home directory—in this case ‘/usr/bill’.
The directory stack is printed whenever there is more than one entry on it andit changes. It is also printed by a dirs command. Dirs is usually faster and more informative than pwdsince it shows the current working directory as well as any other directories remembered in the stack.The pushd command with no argument alternates the current directory with the first directory in thelist.
The ‘pop directory’ popd command without an argument returns you to the directory you were inprior to the current one, discarding the previous current directory from the stack (forgetting it). Typingpopd several times in a series takes you backward through the directories you had been in (changed to) bypushd command. There are other options to pushd and popd to manipulate the contents of the directorystack and to change to directories not at the top of the stack; see the csh manual page for details.Since the shell remembers the working directory in which each job was started, it warns you whenyou might be confused by restarting a job in the foreground which has a different working directory thanthe current working directory of the shell.
Thus if you start a background job, then change the shell’s working directory and then cause the background job to run in the foreground, the shell warns you that the working directory of the currently running foreground job is different from that of the shell.% dirs −l/mnt/bill% cd myproject% dirs˜/myproject% ed prog.c1143ˆZStopped% cd ..% lsmyprojecttextfile% fged prog.c (wd: ˜/myproject)This way the shell warns you when there is an implied change of working directory, even though no cdcommand was issued.