home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / 1_1_contri / usd / 04_csh / csh_2 < prev    next >
Encoding:
Text File  |  1986-05-23  |  36.1 KB  |  1,279 lines

  1. .\" Copyright (c) 1980 Regents of the University of California.
  2. .\" All rights reserved.  The Berkeley software License Agreement
  3. .\" specifies the terms and conditions for redistribution.
  4. .\"
  5. .\"    @(#)csh.2    6.1 (Berkeley) 5/23/86
  6. .\"
  7. .nr H1 1
  8. .NH
  9. Details on the shell for terminal users
  10. .NH 2
  11. Shell startup and termination
  12. .PP
  13. When you login, the shell is started by the system in your
  14. .I home
  15. directory and begins by reading commands from a file
  16. .I \&.cshrc
  17. in this directory.
  18. All shells which you may start during your terminal session will
  19. read from this file.
  20. We will later see what kinds of commands are usefully placed there.
  21. For now we need not have this file and the shell does not complain about
  22. its absence.
  23. .PP
  24. .I "login shell" ,
  25. executed after you login to the system,
  26. will, after it reads commands from
  27. .I \&.cshrc,
  28. read commands from a file
  29. .I \&.login
  30. also in your home directory.
  31. This file contains commands which you wish to do each time you login
  32. to the \s-2UNIX\s0 system.
  33. My
  34. .I \&.login
  35. file looks something like:
  36. .DS
  37. set ignoreeof
  38. set mail=(/usr/spool/mail/bill)
  39. echo "${prompt}users" ; users
  40. alias ts \e
  41.     \'set noglob ; eval \`tset \-s \-m dialup:c100rv4pna \-m plugboard:?hp2621nl \!*\`\';
  42. ts; stty intr ^C kill ^U crt
  43. set time=15 history=10
  44. msgs \-f
  45. if (\-e $mail) then
  46.     echo "${prompt}mail"
  47.     mail
  48. endif
  49. .DE
  50. .PP
  51. This file contains several commands to be executed by \s-2UNIX\s0
  52. each time I login.
  53. The first is a
  54. .I set
  55. command which is interpreted directly by the shell.  It sets the shell
  56. variable
  57. .I ignoreeof
  58. which causes the shell to not log me off if I hit ^D.  Rather,
  59. I use the
  60. .I logout
  61. command to log off of the system.
  62. By setting the
  63. .I mail
  64. variable, I ask the shell to watch for incoming mail to me.  Every 5 minutes
  65. the shell looks for this file and tells me if more mail has arrived there.
  66. An alternative to this is to put the command
  67. .DS
  68. biff y
  69. .DE
  70. in place of this
  71. .I set;
  72. this will cause me to be notified immediately when mail arrives, and to
  73. be shown the first few lines of the new message.
  74. .PP
  75. Next I set the shell variable `time' to `15' causing the shell to automatically
  76. print out statistics lines for commands which execute for at least 15 seconds
  77. of \s-2CPU\s+2 time.  The variable `history' is set to 10 indicating that
  78. I want the shell to remember the last 10 commands I type in its
  79. .I "history list" ,
  80. (described later).
  81. .PP
  82. I create an
  83. .I alias
  84. ``ts'' which executes a
  85. \fItset\fR\|(1) command setting up the modes of the terminal.
  86. The parameters to
  87. .I tset
  88. indicate the kinds of terminal which I usually use when not on a hardwired
  89. port.  I then execute ``ts'' and also use the
  90. .I stty
  91. command to change the interrupt character to ^C and the line kill
  92. character to ^U.
  93. .PP
  94. I then run the `msgs' program, which provides me with any
  95. system messages which I have not seen before; the `\-f' option here prevents
  96. it from telling me anything if there are no new messages.
  97. Finally, if my mailbox file exists, then I run the `mail' program to
  98. process my mail.
  99. .PP
  100. When the `mail' and `msgs' programs finish, the shell will finish
  101. processing my
  102. .I \&.login
  103. file and begin reading commands from the terminal, prompting for each with
  104. `% '.
  105. When I log off (by giving the 
  106. .I logout
  107. command) the shell
  108. will print `logout' and execute commands from the file `.logout'
  109. if it exists in my home directory.
  110. After that the shell will terminate and \s-2UNIX\s0 will log
  111. me off the system.
  112. If the system is not going down, I will receive a new login message.
  113. In any case, after the `logout' message the shell is committed to terminating
  114. and will take no further input from my terminal.
  115. .NH 2
  116. Shell variables
  117. .PP
  118. The shell maintains a set of
  119. .I variables.
  120. We saw above the variables
  121. .I history
  122. and
  123. .I time
  124. which had values `10' and `15'.
  125. In fact, each shell variable has as value an array of
  126. zero or more
  127. .I strings.
  128. Shell variables may be assigned values by the set command.  It has
  129. several forms, the most useful of which was given above and is
  130. .DS
  131. set name=value
  132. .DE
  133. .PP
  134. Shell variables may be used to store values which are to
  135. be used in commands later through a substitution mechanism.
  136. The shell variables most commonly referenced are, however, those which the
  137. shell itself refers to.
  138. By changing the values of these variables one can directly affect the
  139. behavior of the shell.
  140. .PP
  141. One of the most important variables is the variable
  142. .I path.
  143. This variable contains a sequence of directory names where the shell
  144. searches for commands.
  145. The
  146. .I set
  147. command with no arguments
  148. shows the value of all variables currently defined (we usually say
  149. .I set)
  150. in the shell.
  151. The default value for path will be shown by
  152. .I set
  153. to be
  154. .DS
  155. % set
  156. .ta .75i
  157. argv    ()
  158. cwd    /usr/bill
  159. home    /usr/bill
  160. path    (. /usr/ucb /bin /usr/bin)
  161. prompt    %
  162. shell    /bin/csh
  163. status    0
  164. term    c100rv4pna
  165. user    bill
  166. %
  167. .so tabs
  168. .DE
  169. This output indicates that the variable path points to the current
  170. directory `.' and then `/usr/ucb', `/bin' and `/usr/bin'.
  171. Commands which you may write might be in `.' (usually one of
  172. your directories).
  173. Commands developed at Berkeley, live in `/usr/ucb'
  174. while commands developed at Bell Laboratories live in `/bin' and `/usr/bin'.
  175. .PP
  176. A number of locally developed programs on the system live in the directory
  177. `/usr/local'.
  178. If we wish that all shells which we invoke to have
  179. access to these new programs we can place the command
  180. .DS
  181. set path=(. /usr/ucb /bin /usr/bin /usr/local)
  182. .DE
  183. in our file
  184. .I \&.cshrc
  185. in our home directory.
  186. Try doing this and then logging out and back in and do
  187. .DS
  188. set
  189. .DE
  190. again to see that the value assigned to
  191. .I path
  192. has changed.
  193. .FS \(dg
  194. Another directory that might interest you is /usr/new, which contains
  195. many useful user-contributed programs provided with Berkeley Unix.
  196. .FE
  197. .PP
  198. One thing you should be aware of is that the shell examines each directory
  199. which you insert into your path and determines which commands are contained
  200. there.  Except for the current directory `.', which the shell treats specially,
  201. this means that if commands are added to a directory in your search path after
  202. you have started the shell, they will not necessarily be found by the shell.
  203. If you wish to use a command which has been added in this way, you should
  204. give the command
  205. .DS
  206. rehash
  207. .DE
  208. to the shell, which will cause it to recompute its internal table of command
  209. locations, so that it will find the newly added command.
  210. Since the shell has to look in the current directory `.' on each command,
  211. placing it at the end of the path specification usually works equivalently
  212. and reduces overhead.
  213. .PP
  214. Other useful built in variables are the variable
  215. .I home
  216. which shows your home directory,
  217. .I cwd
  218. which contains your current working directory,
  219. the variable
  220. .I ignoreeof
  221. which can be set in your
  222. .I \&.login
  223. file to tell the shell not to exit when it receives an end-of-file from
  224. a terminal (as described above).
  225. The variable `ignoreeof'
  226. is one of several variables which the shell does not care about the
  227. value of, only whether they are
  228. .I set
  229. or
  230. .I unset.
  231. Thus to set this variable you simply do
  232. .DS
  233. set ignoreeof
  234. .DE
  235. and to unset it do
  236. .DS
  237. unset ignoreeof
  238. .DE
  239. These give the variable `ignoreeof' no value, but none is desired or required.
  240. .PP
  241. Finally, some other built-in shell variables of use are the
  242. variables
  243. .I noclobber
  244. and
  245. .I mail.
  246. The metasyntax
  247. .DS
  248. > filename
  249. .DE
  250. which redirects the standard output of a command
  251. will overwrite and destroy the previous contents of the named file.
  252. In this way you may accidentally overwrite a file which is valuable.
  253. If you would prefer that the shell not overwrite files in this
  254. way you can
  255. .DS
  256. set noclobber
  257. .DE
  258. in your
  259. .I \&.login
  260. file.
  261. Then trying to do
  262. .DS
  263. date > now
  264. .DE
  265. would cause a diagnostic if `now' existed already.
  266. You could type
  267. .DS
  268. date >!  now
  269. .DE
  270. if you really wanted to overwrite the contents of `now'.
  271. The `>!' is a special metasyntax indicating that clobbering the
  272. file is ok.\(dg
  273. .FS
  274. \(dgThe space between the `!' and the word `now' is critical here, as `!now'
  275. would be an invocation of the
  276. .I history
  277. mechanism, and have a totally different effect.
  278. .FE
  279. .NH 2
  280. The shell's history list
  281. .PP
  282. The shell can maintain a
  283. .I "history list"
  284. into which it places the words
  285. of previous commands.
  286. It is possible to use a notation to reuse commands or words
  287. from commands in forming new commands.
  288. This mechanism can be used to repeat previous commands or to
  289. correct minor typing mistakes in commands.
  290. .PP
  291. The following figure gives a sample session involving typical usage of the
  292. history mechanism of the shell.
  293. .KF
  294. .DS
  295. % cat bug.c
  296. main()
  297.  
  298. {
  299.     printf("hello);
  300. }
  301. % cc !$
  302. cc bug.c 
  303. "bug.c", line 4: newline in string or char constant
  304. "bug.c", line 5: syntax error
  305. % ed !$
  306. ed bug.c 
  307. 29
  308. 4s/);/"&/p
  309.         printf("hello");
  310. w
  311. 30
  312. q
  313. % !c
  314. cc bug.c 
  315. % a.out
  316. hello% !e
  317. ed bug.c 
  318. 30
  319. 4s/lo/lo\e\en/p
  320.         printf("hello\en");
  321. w
  322. 32
  323. q
  324. % !c \-o bug
  325. cc bug.c \-o bug
  326. % size a.out bug
  327. a.out: 2784+364+1028 = 4176b = 0x1050b
  328. bug: 2784+364+1028 = 4176b = 0x1050b
  329. % ls \-l !*
  330. ls \-l a.out bug 
  331. \(mirwxr\(mixr\(mix 1 bill       3932 Dec 19 09:41 a.out
  332. \(mirwxr\(mixr\(mix 1 bill       3932 Dec 19 09:42 bug
  333. % bug
  334. hello
  335. % num bug.c | spp
  336. spp: Command not found.
  337. % ^spp^ssp
  338. num bug.c | ssp 
  339.     1    main()
  340.     3    {
  341.     4        printf("hello\en");
  342.     5    }
  343. % !! | lpr
  344. num bug.c | ssp | lpr
  345. .DE
  346. .KE
  347. In this example we have a very simple C program which has a bug (or two)
  348. in it in the file `bug.c', which we `cat' out on our terminal.  We then
  349. try to run the C compiler on it, referring to the file again as `!$',
  350. meaning the last argument to the previous command.  Here the `!' is the
  351. history mechanism invocation metacharacter, and the `$' stands for the last
  352. argument, by analogy to `$' in the editor which stands for the end of the line.
  353. The shell echoed the command, as it would have been typed without use of
  354. the history mechanism, and then executed it.
  355. The compilation yielded error diagnostics so we now run the editor on the
  356. file we were trying to compile, fix the bug, and run the C compiler again,
  357. this time referring to this command simply as `!c', which repeats the last
  358. command which started with the letter `c'.  If there were other
  359. commands starting with `c' done recently we could have said `!cc' or even
  360. `!cc:p' which would have printed the last command starting with `cc'
  361. without executing it.
  362. .PP
  363. After this recompilation, we ran the resulting `a.out' file, and then
  364. noting that there still was a bug, ran the editor again.  After fixing
  365. the program we ran the C compiler again, but tacked onto the command
  366. an extra `\-o bug' telling the compiler to place the resultant binary in
  367. the file `bug' rather than `a.out'.  In general, the history mechanisms
  368. may be used anywhere in the formation of new commands and other characters
  369. may be placed before and after the substituted commands.
  370. .PP
  371. We then ran the `size' command to see how large the binary program images
  372. we have created were, and then an `ls \-l' command with the same argument
  373. list, denoting the argument list `\!*'.
  374. Finally we ran the program `bug' to see that its output is indeed correct.
  375. .PP
  376. To make a numbered listing of the program we ran the `num' command on the file `bug.c'.
  377. In order to compress out blank lines in the output of `num' we ran the
  378. output through the filter `ssp', but misspelled it as spp.  To correct this
  379. we used a shell substitute, placing the old text and new text between `^'
  380. characters.  This is similar to the substitute command in the editor.
  381. Finally, we repeated the same command with `!!', but sent its output to the
  382. line printer.
  383. .PP
  384. There are other mechanisms available for repeating commands.  The
  385. .I history
  386. command prints out a number of previous commands with numbers by which
  387. they can be referenced.  There is a way to refer to a previous command
  388. by searching for a string which appeared in it, and there are other,
  389. less useful, ways to select arguments to include in a new command.
  390. A complete description of all these mechanisms
  391. is given in the C shell manual pages in the \s-2UNIX\s0 Programmer's Manual.
  392. .NH 2
  393. Aliases
  394. .PP
  395. The shell has an
  396. .I alias
  397. mechanism which can be used to make transformations on input commands.
  398. This mechanism can be used to simplify the commands you type,
  399. to supply default arguments to commands,
  400. or to perform transformations on commands and their arguments.
  401. The alias facility is similar to a macro facility.
  402. Some of the features obtained by aliasing can be obtained also
  403. using shell command files, but these take place in another instance
  404. of the shell and cannot directly affect the current shells environment
  405. or involve commands such as
  406. .I cd
  407. which must be done in the current shell.
  408. .PP
  409. As an example, suppose that there is a new version of the mail program
  410. on the system called `newmail'
  411. you wish to use, rather than the standard mail program which is called
  412. `mail'.
  413. If you place the shell command
  414. .DS
  415. alias mail newmail
  416. .DE
  417. in your
  418. .I \&.cshrc
  419. file, the shell will transform an input line of the form
  420. .DS
  421. mail bill
  422. .DE
  423. into a call on `newmail'.
  424. More generally, suppose we wish the command `ls' to always show
  425. sizes of files, that is to always do `\-s'.
  426. We can do
  427. .DS
  428. alias ls ls \-s
  429. .DE
  430. or even
  431. .DS
  432. alias dir ls \-s
  433. .DE
  434. creating a new command syntax `dir'
  435. which does an `ls \-s'.
  436. If we say
  437. .DS
  438. dir ~bill
  439. .DE
  440. then the shell will translate this to
  441. .DS
  442. ls \-s /mnt/bill
  443. .DE
  444. .PP
  445. Thus the
  446. .I alias
  447. mechanism can be used to provide short names for commands,
  448. to provide default arguments,
  449. and to define new short commands in terms of other commands.
  450. It is also possible to define aliases which contain multiple
  451. commands or pipelines, showing where the arguments to the original
  452. command are to be substituted using the facilities of the
  453. history mechanism.
  454. Thus the definition
  455. .DS
  456. alias cd \'cd \e!* ; ls \'
  457. .DE
  458. would do an
  459. .I ls
  460. command after each change directory
  461. .I cd
  462. command.
  463. We enclosed the entire alias definition in `\'' characters to prevent
  464. most substitutions from occurring and the character `;' from being
  465. recognized as a metacharacter.
  466. The `!' here is escaped with a `\e' to prevent it from being interpreted
  467. when the alias command is typed in.
  468. The `\e!*' here substitutes the entire argument list to the pre-aliasing
  469. .I cd
  470. command, without giving an error if there were no arguments.
  471. The `;' separating commands is used here
  472. to indicate that one command is to be done and then the next.
  473. Similarly the definition
  474. .DS
  475. alias whois \'grep \e!^ /etc/passwd\'
  476. .DE
  477. defines a command which looks up its first argument in the password file.
  478. .PP
  479. .B Warning:
  480. The shell currently reads the
  481. .I \&.cshrc
  482. file each time it starts up.  If you place a large number of commands
  483. there, shells will tend to start slowly.  A mechanism for saving the shell
  484. environment after reading the \fI\&.cshrc\fR file and quickly restoring it is
  485. under development, but for now you should try to limit the number of
  486. aliases you have to a reasonable number... 10 or 15 is reasonable,
  487. 50 or 60 will cause a noticeable delay in starting up shells, and make
  488. the system seem sluggish when you execute commands from within the editor
  489. and other programs.
  490. .NH 2
  491. More redirection; >> and >&
  492. .PP
  493. There are a few more notations useful to the terminal user
  494. which have not been introduced yet.
  495. .PP
  496. In addition to the standard output, commands also have a
  497. .I "diagnostic output"
  498. which is normally directed to the terminal even when the standard output
  499. is redirected to a file or a pipe.
  500. It is occasionally desirable to direct the diagnostic output along with
  501. the standard output.
  502. For instance if you want to redirect the output of a long running command
  503. into a file and wish to have a record of any error diagnostic it produces
  504. you can do
  505. .DS
  506. command >& file
  507. .DE
  508. The `>&' here tells the shell to route both the diagnostic output and the
  509. standard output into `file'.
  510. Similarly you can give the command
  511. .DS
  512. command |\|& lpr
  513. .DE
  514. to route both standard and diagnostic output through the pipe
  515. to the line printer daemon
  516. .I lpr.\(dd
  517. .FS
  518. \(dd A command of the form
  519. .br
  520. .ti +5
  521. command >&! file
  522. .br
  523. exists, and is used when
  524. .I noclobber
  525. is set and
  526. .I file
  527. already exists.
  528. .FE
  529. .PP
  530. Finally, it is possible to use the form
  531. .DS
  532. command >> file
  533. .DE
  534. to place output at the end of an existing file.\(dg
  535. .FS
  536. \(dg If
  537. .I noclobber
  538. is set, then an error will result if
  539. .I file
  540. does not exist, otherwise the shell will create
  541. .I file
  542. if it doesn't exist.
  543. A form
  544. .br
  545. .ti +5
  546. command >>! file
  547. .br
  548. makes it not be an error for file to not exist when
  549. .I noclobber
  550. is set.
  551. .FE
  552. .NH 2
  553. Jobs; Background, Foreground, or Suspended
  554. .PP
  555. When one or more commands
  556. are typed together as a pipeline or as a sequence of commands separated by
  557. semicolons, a single
  558. .I job
  559. is created by the shell consisting of these commands together as a unit.
  560. Single commands without pipes or semicolons create the simplest jobs.
  561. Usually, every line typed to the shell creates a job.
  562. Some lines that create jobs (one per line) are
  563. .DS
  564. sort < data
  565. ls \-s | sort \-n | head \-5
  566. mail harold
  567. .DE
  568. .PP
  569. If the metacharacter `&' is typed
  570. at the end of the commands, then the job is started as a
  571. .I background
  572. job.  This means that the shell does not wait for it to complete but
  573. immediately prompts and is ready for another command.  The job runs
  574. .I "in the background"
  575. at the same time that normal jobs, called
  576. .I foreground
  577. jobs, continue to be read and executed by the shell one at a time.
  578. Thus
  579. .DS
  580. du > usage &
  581. .DE
  582. would run the
  583. .I du
  584. program, which reports on the disk usage of your working directory (as well as
  585. any directories below it), put the output into the file `usage' and return
  586. immediately with a prompt for the next command without out waiting for
  587. .I du
  588. to finish.  The
  589. .I du
  590. program would continue executing in the background
  591. until it finished, even though you can type and execute more commands in the
  592. mean time.
  593. When a background
  594. job terminates, a message is typed by the shell just before the next prompt
  595. telling you that the job has completed.
  596. In the following example the
  597. .I du
  598. job finishes sometime during the
  599. execution of the
  600. .I mail
  601. command and its completion is reported just before
  602. the prompt after the
  603. .I mail
  604. job is finished.
  605. .DS
  606. % du > usage &
  607. [1] 503
  608. % mail bill
  609. How do you know when a background job is finished?
  610. EOT
  611. .ta 1.75i
  612. [1] \- Done    du > usage
  613. %
  614. .so tabs
  615. .DE
  616. If the job did not terminate normally the `Done' message might say
  617. something else like `Killed'.
  618. If you want the 
  619. terminations of background jobs to be reported at the time they occur
  620. (possibly interrupting the output of other foreground jobs), you can set
  621. the
  622. .I notify
  623. variable.  In the previous example this would mean that the
  624. `Done' message might have come right in the middle of the message to
  625. Bill.
  626. Background jobs are unaffected by any signals from the keyboard like
  627. the \s-2STOP\s0, \s-2INTERRUPT\s0, or \s-2QUIT\s0 signals mentioned earlier.
  628. .PP
  629. Jobs are recorded in a table inside the shell until they terminate.
  630. In this table, the shell remembers the command names, arguments and the
  631. .I "process numbers"
  632. of all commands in the job as well as the working directory where the job was
  633. started.
  634. Each job in the table is either running
  635. .I "in the foreground"
  636. with the shell waiting for it to terminate, running
  637. .I "in the background,"
  638. or
  639. .I suspended.
  640. Only one job can be running in the foreground at one time, but several
  641. jobs can be suspended or running in the background at once.  As each job
  642. is started, it is assigned a small identifying
  643. number called the
  644. .I "job number"
  645. which can be used later to refer to the job in the commands described below.
  646. Job numbers remain
  647. the same until the job terminates and then are re-used.
  648. .PP
  649. When a job is started in the backgound using `&', its number, as well
  650. as the process numbers of all its (top level) commands, is typed by the shell
  651. before prompting you for another command. For example,
  652. .DS
  653. % ls \-s | sort \-n > usage &
  654. [2] 2034 2035
  655. %
  656. .DE
  657. runs the `ls' program with the `\-s' options, pipes this output into
  658. the `sort' program with the `\-n' option which puts its output into the
  659. file `usage'.
  660. Since the `&' was at the end of the line, these two programs were started
  661. together as a background job.  After starting the job, the shell prints
  662. the job number in brackets (2 in this case) followed by the process number
  663. of each program started in the job.  Then the shell immediates prompts for
  664. a new command, leaving the job running simultaneously.
  665. .PP
  666. As mentioned in section 1.8, foreground jobs become
  667. .I suspended
  668. by typing ^Z
  669. which sends a \s-2STOP\s0 signal to the currently running
  670. foreground job.  A background job can become suspended by using the
  671. .I stop
  672. command described below.  When jobs are suspended they merely stop
  673. any further progress until started again, either in the foreground
  674. or the backgound.  The shell notices when a job becomes stopped and
  675. reports this fact, much like it reports the termination of background jobs.
  676. For foreground jobs this looks like
  677. .DS
  678. % du > usage
  679. ^Z
  680. Stopped
  681. %
  682. .DE
  683. `Stopped' message is typed by the shell when it notices that the
  684. .I du
  685. program stopped.
  686. For background jobs, using the
  687. .I stop
  688. command, it is
  689. .DS
  690. % sort usage &
  691. [1] 2345
  692. % stop %1
  693. .ta 1.75i
  694. [1] + Stopped (signal)    sort usage
  695. %
  696. .so tabs
  697. .DE
  698. Suspending foreground jobs can be very useful when you need to temporarily
  699. change what you are doing (execute other commands) and then return to
  700. the suspended job.  Also, foreground jobs can be suspended and then
  701. continued as background jobs using the
  702. .I bg
  703. command, allowing you to continue other work and
  704. stop waiting for the foreground job to finish.  Thus
  705. .DS
  706. % du > usage
  707. ^Z
  708. Stopped
  709. % bg
  710. [1] du > usage &
  711. %
  712. .DE
  713. starts `du' in the foreground, stops it before it finishes, then continues
  714. it in the background allowing more foreground commands to be executed.
  715. This is especially helpful
  716. when a foreground job ends up taking longer than you expected and you
  717. wish you had started it in the backgound in the beginning.
  718. .PP
  719. All
  720. .I "job control"
  721. commands can take an argument that identifies a particular
  722. job.
  723. All job name arguments begin with the character `%', since some of the
  724. job control commands also accept process numbers (printed by the
  725. .I ps
  726. command.)
  727. The default job (when no argument is given) is called the
  728. .I current
  729. job and is identified by a `+' in the output of the
  730. .I jobs
  731. command, which shows you which jobs you have.
  732. When only one job is stopped or running in the background (the usual case)
  733. it is always the current job thus no argument is needed.
  734. If a job is stopped while running in the foreground it becomes the
  735. .I current
  736. job and the existing current job becomes the
  737. .I previous
  738. job \- identified by a `\-' in the output of
  739. .I jobs.
  740. When the current job terminates, the previous job becomes the current job.
  741. When given, the argument is either `%\-' (indicating
  742. the previous job); `%#', where # is the job number; 
  743. `%pref' where pref is some unique prefix of the command name
  744. and arguments of one of the jobs; or `%?' followed by some string found
  745. in only one of the jobs.
  746. .PP
  747. The
  748. .I jobs
  749. command types the table of jobs, giving the job number,
  750. commands and status (`Stopped' or `Running') of each backgound or
  751. suspended job.  With the `\-l' option the process numbers are also
  752. typed.
  753. .DS
  754. % du > usage &
  755. [1] 3398
  756. % ls \-s | sort \-n > myfile &
  757. [2] 3405
  758. % mail bill
  759. ^Z
  760. Stopped
  761. % jobs
  762. .ta 1.75i
  763. [1] \(mi Running    du > usage
  764. [2]    Running    ls \-s | sort \-n > myfile
  765. [3] \(pl Stopped    mail bill
  766. % fg %ls
  767. ls \-s | sort \-n > myfile
  768. % more myfile
  769. .so tabs
  770. .DE
  771. .PP
  772. The
  773. .I fg
  774. command runs a suspended or background job in the foreground.  It is
  775. used to restart a previously suspended job or change a background job
  776. to run in the foreground (allowing signals or input from the terminal).
  777. In the above example we used
  778. .I fg
  779. to change the `ls' job from the
  780. background to the foreground since we wanted to wait for it to
  781. finish before looking at its output file.
  782. The
  783. .I bg
  784. command runs a suspended job in the background.  It is usually used
  785. after stopping the currently running foreground job with the
  786. \s-2STOP\s0 signal.  The combination of the \s-2STOP\s0 signal and the
  787. .I bg
  788. command changes a foreground job into a background job.
  789. The
  790. .I stop
  791. command suspends a background job.
  792. .PP
  793. The
  794. .I kill
  795. command terminates a background or suspended job immediately.
  796. In addition to jobs, it may be given process numbers as arguments,
  797. as printed by
  798. .I ps.
  799. Thus, in the example above, the running
  800. .I du
  801. command could have been terminated by the command
  802. .DS
  803. % kill %1
  804. .ta 1.75i
  805. [1]  Terminated    du > usage
  806. %
  807. .so tabs
  808. .DE
  809. .PP
  810. The
  811. .I notify
  812. command (not the variable mentioned earlier) indicates that the termination
  813. of a specific job should be
  814. reported at the time it finishes instead of waiting for the next prompt.
  815. .PP
  816. If a job running in the background tries to read input from the terminal
  817. it is automatically stopped.  When such a job is then run in the
  818. foreground, input can be given to the job.  If desired, the job can
  819. be run in the background again until it requests input again.
  820. This is illustrated in the following sequence where the `s' command in the
  821. text editor might take a long time.
  822. .ID
  823. .nf
  824. % ed bigfile
  825. 120000
  826. 1,$s/thisword/thatword/
  827. ^Z
  828. Stopped
  829. % bg
  830. [1] ed bigfile &
  831.  . . .  some foreground commands
  832. .ta 1.75i
  833. [1] Stopped (tty input)    ed bigfile
  834. % fg
  835. ed bigfile
  836. w
  837. 120000
  838. q
  839. .so tabs
  840. .DE
  841. So after the `s' command was issued, the `ed' job was stopped with ^Z
  842. and then put in the background using
  843. .I bg.
  844. Some time later when the `s' command was finished,
  845. .I ed
  846. tried to read another command and was stopped because jobs
  847. in the backgound cannot read from the terminal.  The
  848. .I fg
  849. command returned the `ed' job to the foreground where it could once again
  850. accept commands from the terminal.
  851. .PP
  852. The command
  853. .DS
  854. stty tostop
  855. .DE
  856. causes all background jobs run on your terminal to stop
  857. when they are about to
  858. write output to the terminal.  This prevents messages from background
  859. jobs from interrupting foreground job output and allows you to run
  860. a job in the background without losing terminal output.  It also
  861. can be used for interactive programs that sometimes have long
  862. periods without interaction.  Thus each time it outputs a prompt for more
  863. input it will stop before the prompt.  It can then be run in the
  864. foreground using
  865. .I fg,
  866. more input can be given and, if necessary stopped and returned to
  867. the background.  This
  868. .I stty
  869. command might be a good thing to put in your
  870. .I \&.login
  871. file if you do not like output from background jobs interrupting
  872. your work.  It also can reduce the need for redirecting the output
  873. of background jobs if the output is not very big:
  874. .DS
  875. % stty tostop
  876. % wc hugefile &
  877. [1] 10387
  878. % ed text
  879. \&. . . some time later
  880. q
  881. .ta 1.75i
  882. [1] Stopped (tty output)    wc hugefile
  883. % fg wc
  884. wc hugefile
  885.    13371   30123   302577
  886. % stty \-tostop
  887. .so tabs
  888. .DE
  889. Thus after some time the `wc' command, which counts the lines, words
  890. and characters in a file, had one line of output.  When it tried to
  891. write this to the terminal it stopped.  By restarting it in the
  892. foreground we allowed it to write on the terminal exactly when we were
  893. ready to look at its output.
  894. Programs which attempt to change the mode of the terminal will also
  895. block, whether or not
  896. .I tostop
  897. is set, when they are not in the foreground, as
  898. it would be very unpleasant to have a background job change the state
  899. of the terminal.
  900. .PP
  901. Since the
  902. .I jobs
  903. command only prints jobs started in the currently executing shell,
  904. it knows nothing about background jobs started in other login sessions
  905. or within shell files.  The
  906. .I ps
  907. can be used in this case to find out about background jobs not started
  908. in the current shell.
  909. .NH 2
  910. Working Directories
  911. .PP
  912. As mentioned in section 1.6, the shell is always in a particular
  913. .I "working directory."
  914. The `change directory' command
  915. .I chdir
  916. (its
  917. short form
  918. .I cd
  919. may also be used)
  920. changes the working directory of the shell,
  921. that is, changes the directory you
  922. are located in.
  923. .PP
  924. It is useful to make a directory for each project you wish to work on
  925. and to place all files related to that project in that directory.
  926. The `make directory' command,
  927. .I mkdir,
  928. creates a new directory.
  929. The
  930. .I pwd
  931. (`print working directory') command
  932. reports the absolute pathname of the working directory of the shell,
  933. that is, the directory you are
  934. located in.
  935. Thus in the example below:
  936. .DS
  937. % pwd
  938. /usr/bill
  939. % mkdir newpaper
  940. % chdir newpaper
  941. % pwd
  942. /usr/bill/newpaper
  943. %
  944. .DE
  945. the user has created and moved to the
  946. directory
  947. .I newpaper.
  948. where, for example, he might
  949. place a group of related files.
  950. .PP
  951. No matter where you have moved to in a directory hierarchy,
  952. you can return to your `home' login directory by doing just
  953. .DS
  954. cd
  955. .DE
  956. with no arguments.
  957. The name `..' always means the directory above the current one in
  958. the hierarchy, thus
  959. .DS
  960. cd ..
  961. .DE
  962. changes the shell's working directory to the one directly above the
  963. current one.
  964. The name `..' can be used in any
  965. pathname, thus,
  966. .DS
  967. cd ../programs
  968. .DE
  969. means
  970. change to the directory `programs' contained in the directory
  971. above the current one.
  972. If you have several directories for different
  973. projects under, say, your home directory,
  974. this shorthand notation
  975. permits you to switch easily between them.
  976. .PP
  977. The shell always remembers the pathname of its current working directory in
  978. the variable
  979. .I cwd.
  980. The shell can also be requested to remember the previous directory when
  981. you change to a new working directory.  If the `push directory' command
  982. .I pushd
  983. is used in place of the
  984. .I cd
  985. command, the shell saves the name of the current working directory
  986. on a
  987. .I "directory stack"
  988. before changing to the new one.
  989. You can see this list at any time by typing the `directories'
  990. command
  991. .I dirs.
  992. .ID
  993. .nf
  994. % pushd newpaper/references
  995. ~/newpaper/references  ~
  996. % pushd /usr/lib/tmac
  997. /usr/lib/tmac  ~/newpaper/references  ~
  998. % dirs
  999. /usr/lib/tmac  ~/newpaper/references  ~
  1000. % popd
  1001. ~/newpaper/references  ~
  1002. % popd
  1003. ~
  1004. %
  1005. .DE
  1006. The list is printed in a horizontal line, reading left to right,
  1007. with a tilde (~) as
  1008. shorthand for your home directory\(emin this case `/usr/bill'.
  1009. The directory stack is printed whenever there is more than one
  1010. entry on it and it changes.
  1011. It is also printed by a
  1012. .I dirs
  1013. command.
  1014. .I Dirs
  1015. is usually faster and more informative than
  1016. .I pwd
  1017. since it shows the current working directory as well as any
  1018. other directories remembered in the stack.
  1019. .PP
  1020. The
  1021. .I pushd
  1022. command with no argument
  1023. alternates the current directory with the first directory in the
  1024. list.
  1025. The `pop directory'
  1026. .I popd
  1027. command without an argument returns you to the directory you were in prior to
  1028. the current one, discarding the previous current directory from the
  1029. stack (forgetting it).
  1030. Typing
  1031. .I popd
  1032. several times in a series takes you backward through the directories
  1033. you had been in (changed to) by
  1034. .I pushd
  1035. command.
  1036. There are other options to
  1037. .I pushd
  1038. and
  1039. .I popd
  1040. to manipulate the contents of the directory stack and to change
  1041. to directories not at the top of the stack; see the
  1042. .I csh
  1043. manual page for details.
  1044. .PP
  1045. Since the shell remembers the working directory in which each job
  1046. was started, it warns you when you might be confused by restarting
  1047. a job in the foreground which has a different working directory than the
  1048. current working directory of the shell.  Thus if you start a background
  1049. job, then change the shell's working directory and then cause the
  1050. background job to run in the foreground, the shell warns you that the
  1051. working directory of the currently running foreground job is different
  1052. from that of the shell.
  1053. .DS
  1054. % dirs \-l
  1055. /mnt/bill
  1056. % cd myproject
  1057. % dirs
  1058. ~/myproject
  1059. % ed prog.c
  1060. 1143
  1061. ^Z
  1062. Stopped
  1063. % cd ..
  1064. % ls
  1065. myproject
  1066. textfile
  1067. % fg
  1068. ed prog.c (wd: ~/myproject)
  1069. .DE
  1070. This way the shell warns you when there
  1071. is an implied change of working directory, even though no cd command was
  1072. issued.  In the above example the `ed' job was still in `/mnt/bill/project'
  1073. even though the shell had changed to `/mnt/bill'.
  1074. A similar warning is given when such a foreground job
  1075. terminates or is suspended (using the \s-2STOP\s0 signal) since
  1076. the return to the shell again implies a change of working directory.
  1077. .DS
  1078. % fg
  1079. ed prog.c (wd: ~/myproject)
  1080.  . . . after some editing
  1081. q
  1082. (wd now: ~)
  1083. %
  1084. .DE
  1085. These messages are sometimes confusing if you use programs that change
  1086. their own working directories, since the shell only remembers which
  1087. directory a job is started in, and assumes it stays there.
  1088. The `\-l' option of
  1089. .I jobs
  1090. will type the working directory
  1091. of suspended or background jobs when it is different
  1092. from the current working directory of the shell.
  1093. .NH 2
  1094. Useful built-in commands
  1095. .PP
  1096. We now give a few of the useful built-in commands of the shell describing
  1097. how they are used.
  1098. .PP
  1099. The
  1100. .I alias
  1101. command described above is used to assign new aliases and to show the
  1102. existing aliases.
  1103. With no arguments it prints the current aliases.
  1104. It may also be given only one argument such as
  1105. .DS
  1106. alias ls
  1107. .DE
  1108. to show the current alias for, e.g., `ls'.
  1109. .PP
  1110. The
  1111. .I echo
  1112. command prints its arguments.
  1113. It is often used in
  1114. .I "shell scripts"
  1115. or as an interactive command
  1116. to see what filename expansions will produce.
  1117. .PP
  1118. The
  1119. .I history
  1120. command will show the contents of the history list.
  1121. The numbers given with the history events can be used to reference
  1122. previous events which are difficult to reference using the
  1123. contextual mechanisms introduced above.
  1124. There is also a shell variable called
  1125. .I prompt.
  1126. By placing a `!' character in its value the shell will there substitute
  1127. the number of the current command in the history list.
  1128. You can use this number to refer to this command in a history substitution.
  1129. Thus you could
  1130. .DS
  1131. set prompt=\'\e! % \'
  1132. .DE
  1133. Note that the `!' character had to be
  1134. .I escaped
  1135. here even within `\'' characters.
  1136. .PP
  1137. The
  1138. .I limit
  1139. command is used to restrict use of resources.
  1140. With no arguments it prints the current limitations:
  1141. .DS
  1142. .ta 1i
  1143. cputime    unlimited
  1144. filesize    unlimited
  1145. datasize    5616 kbytes
  1146. stacksize    512 kbytes
  1147. coredumpsize    unlimited
  1148. .so tabs
  1149. .DE
  1150. Limits can be set, e.g.:
  1151. .DS
  1152. limit coredumpsize 128k
  1153. .DE
  1154. Most reasonable units abbreviations will work; see the
  1155. .I csh
  1156. manual page for more details.
  1157. .PP
  1158. The
  1159. .I logout
  1160. command can be used to terminate a login shell which has
  1161. .I ignoreeof
  1162. set.
  1163. .PP
  1164. The
  1165. .I rehash
  1166. command causes the shell to recompute a table of where commands are
  1167. located.  This is necessary if you add a command to a directory
  1168. in the current shell's search path and wish the shell to find it,
  1169. since otherwise the hashing algorithm may tell the shell that the
  1170. command wasn't in that directory when the hash table was computed.
  1171. .PP
  1172. The
  1173. .I repeat
  1174. command can be used to repeat a command several times.
  1175. Thus to make 5 copies of the file
  1176. .I one
  1177. in the file
  1178. .I five
  1179. you could do
  1180. .DS
  1181. repeat 5 cat one >> five
  1182. .DE
  1183. .PP
  1184. The
  1185. .I setenv
  1186. command can be used
  1187. to set variables in the environment.
  1188. Thus
  1189. .DS
  1190. setenv TERM adm3a
  1191. .DE
  1192. will set the value of the environment variable \s-2TERM\s0
  1193. to
  1194. `adm3a'.
  1195. A user program
  1196. .I printenv
  1197. exists which will print out the environment.
  1198. It might then show:
  1199. .DS
  1200. % printenv
  1201. HOME=/usr/bill
  1202. SHELL=/bin/csh
  1203. PATH=:/usr/ucb:/bin:/usr/bin:/usr/local
  1204. TERM=adm3a
  1205. USER=bill
  1206. %
  1207. .DE
  1208. .PP
  1209. The
  1210. .I source
  1211. command can be used to force the current shell to read commands from
  1212. a file.
  1213. Thus
  1214. .DS
  1215. source .cshrc
  1216. .DE
  1217. can be used after editing in a change to the
  1218. .I \&.cshrc
  1219. file which you wish to take effect right away.
  1220. .PP
  1221. The
  1222. .I time
  1223. command can be used to cause a command to be timed no matter how much
  1224. \s-2CPU\s0 time it takes.
  1225. Thus
  1226. .DS
  1227. % time cp /etc/rc /usr/bill/rc
  1228. 0.0u 0.1s 0:01 8% 2+1k 3+2io 1pf+0w
  1229. % time wc /etc/rc /usr/bill/rc
  1230.      52    178   1347 /etc/rc
  1231.      52    178   1347 /usr/bill/rc
  1232.     104    356   2694 total
  1233. 0.1u 0.1s 0:00 13% 3+3k 5+3io 7pf+0w
  1234. %
  1235. .DE
  1236. indicates that the
  1237. .I cp
  1238. command used a negligible amount of user time (u)
  1239. and about 1/10th of a system time (s); the elapsed time was 1 second (0:01),
  1240. there was an average memory usage of 2k bytes of program space and 1k
  1241. bytes of data space over the cpu time involved (2+1k); the program
  1242. did three disk reads and two disk writes (3+2io), and took one page fault
  1243. and was not swapped (1pf+0w).
  1244. The word count command
  1245. .I wc
  1246. on the other hand used 0.1 seconds of user time and 0.1 seconds of system
  1247. time in less than a second of elapsed time.
  1248. The percentage `13%' indicates that over the period when it was active
  1249. the command `wc' used an average of 13 percent of the available \s-2CPU\s0
  1250. cycles of the machine.
  1251. .PP
  1252. The
  1253. .I unalias
  1254. and
  1255. .I unset
  1256. commands can be used
  1257. to remove aliases and variable definitions from the shell, and
  1258. .I unsetenv
  1259. removes variables from the environment.
  1260. .NH 2
  1261. What else?
  1262. .PP
  1263. This concludes the basic discussion of the shell for terminal users.
  1264. There are more features of the shell to be discussed here, and all
  1265. features of the shell are discussed in its manual pages.
  1266. One useful feature which is discussed later is the
  1267. .I foreach
  1268. built-in command which can be used to run the same command
  1269. sequence with a number of different arguments.
  1270. .PP
  1271. If you intend to use \s-2UNIX\s0 a lot you you should look through
  1272. the rest of this document and the csh manual pages (section1) to become familiar
  1273. with the other facilities which are available to you.
  1274. .bp
  1275.