1 .\" $NetBSD: t2,v 1.3 2010/08/22 02:19:07 perry Exp $
3 .\" Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions are
9 .\" Redistributions of source code and documentation must retain the above
10 .\" copyright notice, this list of conditions and the following
13 .\" Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgment:
20 .\" This product includes software developed or owned by Caldera
21 .\" International, Inc. Neither the name of Caldera International, Inc.
22 .\" nor the names of other contributors may be used to endorse or promote
23 .\" products derived from this software without specific prior written
26 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
27 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
28 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 .\" DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
31 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 .\" @(#)t2 8.1 (Berkeley) 6/8/93
44 The shell may be used to read and execute commands
48 sh file [ args \*(ZZ ]
50 calls the shell to read commands from \fIfile.\fP
51 Such a file is called a \fIshell script.\fP
52 Arguments may be supplied with the call
53 and are referred to in \fIfile\fP
54 using the positional parameters
55 \fB$1, $2, \*(ZZ\|.\fR
57 For example, if the file \fIwg\fP contains
70 UNIX files have three independent attributes,
71 \fIread,\fP \fIwrite\fP and \fIexecute.\fP
72 The UNIX command \fIchmod\fP(1) may be used
73 to make a file executable.
78 will ensure that the file \fIwg\fP has execute status.
79 Following this, the command
87 This allows shell scripts and other programs
88 to be used interchangeably.
89 In either case a new process is created to
92 The `\fB#\fP' character is used as a comment character by the shell.
93 All characters following the `#' on a line are ignored.
95 A typical modern system has several different shells, some with differing
96 command syntax, and it is desirable to specify which one should be
97 invoked when an executable script is invoked.
98 If the special comment
100 #!/\fIpath\fP/\fIto\fP/\fIinterpreter\fP
102 appears as the first line in a script, it is used to specify the
103 absolute pathname of the shell (or other interpreter) that should be
104 used to execute the file.
105 (Without such a line, \fB/bin/sh\fP is assumed.)
106 It is best if a script explicitly states
107 what shell it is intended for in this manner.
109 As well as providing names for the positional
111 the number of positional parameters to a script
112 is available as \fB$#\|.\fP
113 The name of the file being executed
114 is available as \fB$0\|.\fP
116 A special shell parameter \fB$\*(ST\fP
117 is used to substitute for all positional parameters
119 A typical use of this is to provide
120 some default arguments,
123 nroff \(miT450 \(mims $\*(ST
125 which simply prepends some arguments
126 to those already given.
127 (The variable \fB$@\fP also expands to ``all positional
128 parameters'', but is subtly different when expanded inside quotes.
129 See section 3.5, below.)
131 2.1\ Control\ flow\ -\ for
133 A frequent use of shell scripts is to loop
134 through the arguments (\fB$1, $2, \*(ZZ\fR)
135 executing commands once for each argument.
136 An example of such a script is
137 \fItel\fP that searches the file
138 \fB/usr/share/telnos\fR
139 that contains lines of the form
146 The text of \fItel\fP is
152 grep $i /usr/share/telnos
159 prints those lines in \fB/usr/share/telnos\fR
160 that contain the string \fIfred\|.\fP
164 prints those lines containing \fIfred\fP
165 followed by those for \fIbert.\fP
167 The \fBfor\fP loop notation is recognized by the shell
168 and has the general form
170 \fBfor\fR \fIname\fR \fBin\fR \fIw1 w2 \*(ZZ\fR
171 \fBdo\fR \fIcommand-list\fR
174 A \fIcommand-list\fP is a sequence of one or more
175 simple commands separated or terminated by a newline or semicolon.
176 Furthermore, reserved words
177 like \fBdo\fP and \fBdone\fP are only
178 recognized following a newline or
180 \fIname\fP is a shell variable that is set
181 to the words \fIw1 w2 \*(ZZ\fR in turn each time the \fIcommand-list\fP
184 If \fBin\fR \fIw1 w2 \*(ZZ\fR
185 is omitted then the loop
186 is executed once for each positional parameter;
187 that is, \fBin\fR \fI$\*(ST\fR is assumed.
189 Another example of the use of the \fBfor\fP
190 loop is the \fIcreate\fP command
199 ensures that two empty files
200 \fIalpha\fP and \fIbeta\fP exist
202 The notation \fI>file\fP may be used on its
203 own to create or clear the contents of a file.
204 Notice also that a semicolon (or newline) is required before \fBdone.\fP
206 2.2\ Control\ flow\ -\ case
208 A multiple way branch is provided for by the
213 \*(Ca1) cat \*(AP$1 ;;
214 \*(Ca2) cat \*(AP$2 <$1 ;;
215 \*(Ca\*(ST) echo \'usage: append [ from ] to\' ;;
218 is an \fIappend\fP command.
224 \fB$#\fP is the string \fI1\fP and
225 the standard input is copied onto the
227 using the \fIcat\fP command.
231 appends the contents of \fIfile1\fP
233 If the number of arguments supplied to
234 \fIappend\fP is other than 1 or 2
235 then a message is printed indicating
238 The general form of the \fBcase\fP command
241 \fBcase \fIword \fBin
242 \*(Ca\fIpattern\|\fB)\ \fIcommand-list\fB\|;;
246 The shell attempts to match
247 \fIword\fR with each \fIpattern,\fR
248 in the order in which the patterns
250 If a match is found the
251 associated \fIcommand-list\fP is
252 executed and execution
253 of the \fBcase\fP is complete.
254 Since \*(ST is the pattern that matches any
255 string it can be used for the default case.
258 no check is made to ensure that only
261 The first match found defines the set of commands
263 In the example below the commands following
264 the second \*(ST will never be executed.
272 Another example of the use of the \fBcase\fP
273 construction is to distinguish
274 between different forms
276 The following example is a fragment of a \fIcc\fP command.
280 \*(DC\(mi[ocs]) \*(ZZ ;;
281 \*(DC\(mi\*(ST) echo "unknown flag $i" ;;
282 \*(DC\*(ST.c) /lib/c0 $i \*(ZZ ;;
283 \*(DC\*(ST) echo "unexpected argument $i" ;;
288 To allow the same commands to be associated
289 with more than one pattern
290 the \fBcase\fP command provides
291 for alternative patterns
292 separated by a \*(VT\|.
296 \*(Ca\(mix\*(VT\(miy) \*(ZZ
306 The usual quoting conventions apply
312 will match the character \fB?\|.\fP
316 The shell script \fItel\fP
317 in section 2.1 uses the file \fB/usr/share/telnos\fR
320 An alternative is to include this
322 within the shell script as a \fIhere\fP document, as in,
334 the shell takes the lines between \fB\*(HE!\fR and \fB!\fR
335 as the standard input for \fIgrep.\fP
336 The string \fB!\fR is arbitrary, the document
337 being terminated by a line that consists
338 of the string following \*(HE\|.
340 Parameters are substituted in the document
341 before it is made available to \fIgrep\fP
342 as illustrated by the following script
352 edg string1 string2 file
354 is then equivalent to the command
357 g/string1/s//string2/g
361 and changes all occurrences of \fIstring1\fP
362 in \fIfile\fP to \fIstring2\|.\fP
363 Substitution can be prevented using \\
364 to quote the special character \fB$\fP
372 (This version of \fIedg\fP is equivalent to
373 the first except that \fIed\fP will print
374 a \fB?\fR if there are no occurrences of
375 the string \fB$1\|.\fP)
376 Substitution within a \fIhere\fP document
377 may be prevented entirely by quoting
378 the terminating string,
385 The document is presented
386 without modification to \fIgrep.\fP
387 If parameter substitution is not required
388 in a \fIhere\fP document this latter form
391 2.4\ Shell\ variables\(dg
394 Also known as \fIenvironment variables\fB, see \fIenvironment\fB(7).
397 provides string-valued variables.
398 Variable names begin with a letter
399 and consist of letters, digits and
401 Variables may be given values by writing, for example,
403 user=fred\ box=m000\ acct=mh0000
405 which assigns values to the variables
406 \fBuser, box\fP and \fBacct.\fP
407 A variable may be set to the null string
408 by saying, for example,
412 The value of a variable is substituted
413 by preceding its name with \fB$\|\fP;
418 will echo \fIfred.\fP
420 Variables may be used interactively
421 to provide abbreviations for frequently
428 will move the file \fIpgm\fP
429 from the current directory to the directory \fB/usr/fred/bin\|.\fR
430 A more general notation is available for parameter
436 which is equivalent to
440 and is used when the parameter name is
441 followed by a letter or digit.
447 will direct the output of \fIps\fR
448 to the file \fB/tmp/psa,\fR
453 would cause the value of the variable \fBtmpa\fP
456 Except for \fB$?\fP the following
457 are set initially by the shell.
458 \fB$?\fP is set after executing each command.
461 The exit status (return code)
462 of the last command executed
464 Most commands return a zero exit status
465 if they complete successfully,
466 otherwise a non-zero exit status is returned.
467 Testing the value of return codes is dealt with
468 later under \fBif\fP and \fBwhile\fP commands.
470 The number of positional parameters
472 Used, for example, in the \fIappend\fP command
473 to check the number of parameters.
475 The process number of this shell (in decimal).
476 Since process numbers are unique among
477 all existing processes, this string is
478 frequently used to generate
480 temporary file names.
488 The process number of the last process
489 run in the background (in decimal).
491 The current shell flags, such as
492 \fB\(mix\fR and \fB\(miv\|.\fR
495 Some variables have a special meaning to the
496 shell and should be avoided for general
499 .IP \fB$\s-1MAIL\s0\fP 8
500 When used interactively
501 the shell looks at the file
502 specified by this variable
503 before it issues a prompt.
504 If the specified file has been modified
506 was last looked at the shell
508 \fIyou have mail\fP before prompting
509 for the next command.
510 This variable is typically set
511 in the file \fB.profile,\fP
512 in the user's login directory.
515 \s-1MAIL\s0=/usr/spool/mail/fred
517 .IP \fB$\s-1HOME\s0\fP 8
519 for the \fIcd\fP command.
520 The current directory is used to resolve
521 file name references that do not begin with
523 and is changed using the \fIcd\fP command.
528 makes the current directory \fB/usr/fred/bin\|.\fR
532 will print on the terminal the file \fIwn\fP
535 \fIcd\fP with no argument
540 This variable is also typically set in the
541 the user's login profile.
542 .IP \fB$\s-1PWD\s0\fP 8
543 The current working directory. Set by the \fIcd\fB command.
544 .IP \fB$\s-1PATH\s0\fP 8
545 A list of directories that contain commands (the \fIsearch path\fR\|).
546 Each time a command is executed by the shell
547 a list of directories is searched
548 for an executable file.
550 If \fB$\s-1PATH\s0\fP is not set
551 then the current directory,
552 \fB/bin\fP, and \fB/usr/bin\fP are searched by default.
554 Otherwise \fB$\s-1PATH\s0\fP consists of directory
555 names separated by \fB:\|.\fP
558 \s-1PATH\s0=\fB:\fP/usr/fred/bin\fB:\fP/bin\fB:\fP/usr/bin
560 specifies that the current directory
561 (the null string before the first \fB:\fP\|),
562 \fB/usr/fred/bin, /bin \fRand\fP /usr/bin\fR
563 are to be searched in that order.
564 In this way individual users
565 can have their own `private' commands
566 that are accessible independently
567 of the current directory.
568 If the command name contains a \fB/\fR then this directory search
569 is not used; a single attempt
570 is made to execute the command.
571 .IP \fB$\s-1PS1\s0\fP 8
572 The primary shell prompt string, by default, `\fB$\ \fR'.
573 .IP \fB$\s-1PS2\s0\fP 8
574 The shell prompt when further input is needed,
575 by default, `\fB>\ \fR'.
576 .IP \fB$\s-1IFS\s0\fP 8
577 The set of characters used by \fIblank
578 interpretation\fR (see section 3.5).
579 .IP \fB$\s-1ENV\s0\fP 8
580 The shell reads and executes the commands in the file
581 specified by this variable when it is initially started.
582 Unlike the \fB.profile\fP file, these commands are executed by all
583 shells, not just the one started at login.
584 (Most versions of the shell specify a filename that is used if
585 \s-1ENV\s0 is not explicitly set. See the manual page for your shell.)
588 2.5\ The\ test\ command
590 The \fItest\fP command, although not part of the shell,
591 is intended for use by shell programs.
596 returns zero exit status if \fIfile\fP
597 exists and non-zero exit status otherwise.
598 In general \fItest\fP evaluates a predicate
599 and returns the result as its exit status.
600 Some of the more frequently used \fItest\fP
601 arguments are given here, see \fItest\fP(1)
602 for a complete specification.
604 test s true if the argument \fIs\fP is not the null string
605 test \(mif file true if \fIfile\fP exists
606 test \(mir file true if \fIfile\fP is readable
607 test \(miw file true if \fIfile\fP is writable
608 test \(mid file true if \fIfile\fP is a directory
610 The \fItest\fP command is known as `\fB[\fP' and may be invoked as
612 For aesthetic reasons, the command ignores a close bracket `\fB]\fP' given
613 at the end of a command so
621 are completely equivalent.
622 Typically, the bracket notation is used when \fItest\fP is invoked inside
623 shell control constructs.
625 2.6\ Control\ flow\ -\ while
628 the \fBfor\fP loop and the \fBcase\fP
629 branch are determined by data available to the shell.
630 A \fBwhile\fP or \fBuntil\fP loop
631 and an \fBif then else\fP branch
632 are also provided whose
633 actions are determined by the exit status
634 returned by commands.
635 A \fBwhile\fP loop has the general form
637 \fBwhile\fP \fIcommand-list\*1\fP
638 \fBdo\fP \fIcommand-list\*2\fP
642 The value tested by the \fBwhile\fP command
643 is the exit status of the last simple command
644 following \fBwhile.\fP
645 Each time round the loop
646 \fIcommand-list\*1\fP is executed;
647 if a zero exit status is returned then
648 \fIcommand-list\*2\fP
650 otherwise, the loop terminates.
664 \fIshift\fP is a shell command that
665 renames the positional parameters
666 \fB$2, $3, \*(ZZ\fR as \fB$1, $2, \*(ZZ\fR
667 and loses \fB$1\|.\fP
669 Another kind of use for the \fBwhile/until\fP
670 loop is to wait until some
671 external event occurs and then run
673 In an \fBuntil\fP loop
674 the termination condition is reversed.
681 will loop until \fIfile\fP exists.
682 Each time round the loop it waits for
683 5 minutes before trying again.
684 (Presumably another process
685 will eventually create the file.)
687 The most recent enclosing loop may be exited with the \fBbreak\fP
688 command, or the rest of the body skipped and the next iteration begun
689 with the \fBcontinue\fP command.
691 The commands \fItrue\fP(1) and \fIfalse\fP(1) return 0 and non-zero
692 exit statuses respectively. They are sometimes of use in control flow,
699 is an infinite loop that prints the date and time every five seconds.
701 2.7\ Control\ flow\ -\ if
704 general conditional branch
707 \fBif\fP \fIcommand-list
708 \fBthen \fIcommand-list
709 \fBelse \fIcommand-list
712 that tests the value returned by the last simple command
715 The \fBif\fP command may be used
716 in conjunction with the \fItest\fP command
717 to test for the existence of a file as in
720 then \fIprocess file\fP
721 else \fIdo something else\fP
725 An example of the use of \fBif, case\fP
726 and \fBfor\fP constructions is given in
729 A multiple test \fBif\fP command
742 may be written using an extension of the \fBif\fP
754 The following example is an implementation of the \fItouch\fP command
755 which changes the `last modified' time for a list
757 The command may be used in conjunction
758 with \fImake\fP(1) to force recompilation of a list
766 \*(DC\(mic) flag=N ;;
767 \*(DC\*(ST) if [ \(mif $i ]
768 \*(DC then cp $i junk$$; mv junk$$ $i
770 \*(DC then echo file \\'$i\\' does not exist
776 The \fB\(mic\fP flag is used in this command to
777 force subsequent files to be created if they do not already exist.
778 Otherwise, if the file does not exist, an error message is printed.
779 The shell variable \fIflag\fP
780 is set to some non-null string if the \fB\(mic\fP
781 argument is encountered.
786 copy the file and then overwrite it with the copy,
787 thus causing the last modified date to be updated.
801 command1 \*(VT\*(VT command2
803 executes \fIcommand2\fP only if \fIcommand1\fP
805 In each case the value returned
806 is that of the last simple command executed.
808 Placing a `\fB!\fP' in front of a pipeline inverts its exit
809 status, almost in the manner of the C operator of the same name.
814 echo $1 is not a directory
817 will print a message only if $1 is not a directory.
819 2.8\ Command\ grouping
821 Commands may be grouped in two ways,
823 \fB{\fI command-list\fB ; }\fR
827 \fB(\fI command-list\fB )\fR
830 In the first \fIcommand-list\fP is simply executed.
831 The second form executes \fIcommand-list\fP
832 as a separate process.
837 executes \fIrm junk\fP in the directory
838 \fBx\fP without changing the current
839 directory of the invoking shell.
845 have the same effect but leave the invoking
846 shell in the directory \fBx.\fP
848 2.9\ Shell\ Functions
850 A function may be defined by the syntax
852 \fIfuncname\fP() \fB{\fI command-list\fB ; }\fR
854 Functions are invoked within a script as though they were separate
855 commands of the same name.
856 While they are executed, the
857 positional parameters \fB$1, $2, \*(ZZ\fR are temporarily set to the
858 arguments passed to the function. For example:
868 2.10\ Debugging\ shell\ scripts
870 The shell provides two tracing mechanisms
871 to help when debugging shell scripts.
872 The first is invoked within the script
877 (\fBv\fP for verbose) and causes lines of the
878 script to be printed as they are read.
879 It is useful to help isolate syntax errors.
880 It may be invoked without modifying the script
883 sh \(miv \fIscript\fP \*(ZZ
885 where \fIscript\fP is the name of the shell script.
886 This flag may be used in conjunction
887 with the \fB\(min\fP flag which prevents
888 execution of subsequent commands.
889 (Note that saying \fIset \(min\fP at a terminal
890 will render the terminal useless
891 until an end-of-file is typed.)
897 will produce an execution
899 Following parameter substitution
900 each command is printed as it is executed.
901 (Try these at the terminal to see
902 what effect they have.)
903 Both flags may be turned off by saying
907 and the current setting of the shell flags is available as \fB$\(mi\|\fR.
909 2.11\ The\ man\ command
911 The following is a simple implementation of the \fIman\fP command,
912 which is used to display sections of the UNIX manual on your terminal.
913 It is called, for example, as
919 In the first the manual section for \fIsh\fP
921 Since no section is specified, section 1 is used.
922 The second example will typeset (\fB\(mit\fP option)
923 the manual section for \fIed.\fP
924 The last prints the \fIfork\fP manual page
925 from section 2, which covers system calls.
932 # "#" is the comment character
933 # default is nroff ($N), section 1 ($s)
939 \*(DC[1\(mi9]\*(ST) s=$i ;;
945 \*(DC\(mi\*(ST) echo unknown flag \\'$i\\' ;;
947 \*(DC\*(ST) if [ \(mif man$s/$i.$s ]
949 \*(DC ${N}roff \(miman man$s/$i.$s
950 \*(DC else # look through all manual sections
952 \*(DC for j in 1 2 3 4 5 6 7 8 9
954 \*(DC \*(DOif [ \(mif man$j/$i.$j ]
956 \*(DC \*(DO\*(THman $j $i
957 \*(DC \*(DO\*(THfound=yes
958 \*(DC \*(DO\*(THbreak
962 \*(DC \*(Cano) echo \\'$i: manual page not found\\'
970 Figure 1. A version of the man command