home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / usd / 03_shell / t2_txt < prev    next >
Encoding:
Text File  |  1996-10-12  |  18.2 KB  |  859 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. 2.0 Shell procedures
  8.  
  9. The shell may be used to read and execute commands contained
  10. in a file.  For example,
  11.  
  12.           sh file [ args  ]
  13.  
  14. calls the shell to read commands from file.  Such a file  is
  15. called  a  command  procedure or shell procedure.  Arguments
  16. may be supplied with the call and are referred  to  in  file
  17. using  the  positional parameters $1, $2, .  For example, if
  18. the file wg contains
  19.  
  20.           who  grep $1
  21.  
  22. then
  23.  
  24.           sh wg fred
  25.  
  26. is equivalent to
  27.  
  28.           who  grep fred
  29.  
  30.  
  31. UNIX files have three independent  attributes,  read,  write
  32. and execute.  The UNIX command chmod (1) may be used to make
  33. a file executable.  For example,
  34.  
  35.           chmod +x wg
  36.  
  37. will ensure that the file wg has execute status.   Following
  38. this, the command
  39.  
  40.           wg fred
  41.  
  42. is equivalent to
  43.  
  44.           sh wg fred
  45.  
  46. This  allows shell procedures and programs to be used inter-
  47. changeably.  In either case a new process is created to  run
  48. the command.
  49.  
  50. As  well  as  providing names for the positional parameters,
  51. the number of positional parameters in the call is available
  52. as  $#.  The name of the file being executed is available as
  53. $0.
  54.  
  55. A special shell parameter $ is used to  substitute  for  all
  56. positional  parameters  except $0.  A typical use of this is
  57. to provide some default arguments, as in,
  58.  
  59.           nroff -T450 -ms $
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                              -2-
  71.  
  72.  
  73. which simply prepends some arguments to those already given.
  74.  
  75. 2.1 Control flow - for
  76.  
  77. A  frequent  use  of shell procedures is to loop through the
  78. arguments ($1, $2, ) executing commands once for each  argu-
  79. ment.   An  example of such a procedure is tel that searches
  80. the file /usr/lib/telnos that contains lines of the form
  81.  
  82.  
  83.           fred mh0123
  84.           bert mh0789
  85.  
  86.  
  87. The text of tel is
  88.  
  89.           for i
  90.           do grep $i /usr/lib/telnos; done
  91.  
  92. The command
  93.  
  94.           tel fred
  95.  
  96. prints those  lines  in  /usr/lib/telnos  that  contain  the
  97. string fred.
  98.  
  99.           tel fred bert
  100.  
  101. prints  those  lines  containing  fred followed by those for
  102. bert.
  103.  
  104. The for loop notation is recognized by the shell and has the
  105. general form
  106.  
  107.           for name in w1 w2
  108.           do command-list
  109.           done
  110.  
  111. A  command-list is a sequence of one or more simple commands
  112. separated or terminated by a newline or semicolon.  Further-
  113. more,  reserved  words  like do and done are only recognized
  114. following a newline or semicolon.  name is a shell  variable
  115. that  is  set to the words w1 w2  in turn each time the com-
  116. mand-list following do is executed.  If in w1 w2 is  omitted
  117. then  the  loop is executed once for each positional parame-
  118. ter; that is, in $ is assumed.
  119.  
  120. Another example of the use of the for  loop  is  the  create
  121. command whose text is
  122.  
  123.           for i do >$i; done
  124.  
  125. The command
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                              -3-
  137.  
  138.  
  139.           create alpha beta
  140.  
  141. ensures  that  two  empty files alpha and beta exist and are
  142. empty.  The notation >file may be used on its own to  create
  143. or  clear  the contents of a file.  Notice also that a semi-
  144. colon (or newline) is required before done.
  145.  
  146. 2.2 Control flow - case
  147.  
  148. A multiple way branch is provided for by the case  notation.
  149. For example,
  150.  
  151.           case $# in
  152.           1)   cat $1 ;;
  153.           2)   cat $2 <$1 ;;
  154.           )    echo \'usage: append [ from ] to\' ;;
  155.           esac
  156.  
  157. is an append command.  When called with one argument as
  158.  
  159.           append file
  160.  
  161. $# is the string 1 and the standard input is copied onto the
  162. end of file using the cat command.
  163.  
  164.           append file1 file2
  165.  
  166. appends the contents of file1 onto file2.  If the number  of
  167. arguments  supplied  to  append  is other than 1 or 2 then a
  168. message is printed indicating proper usage.
  169.  
  170. The general form of the case command is
  171.  
  172.           case word in
  173.           pattern) command-list;;
  174.  
  175.           esac
  176.  
  177. The shell attempts to match word with each pattern,  in  the
  178. order in which the patterns appear.  If a match is found the
  179. associated command-list is executed  and  execution  of  the
  180. case  is  complete.   Since  is the pattern that matches any
  181. string it can be used for the default case.
  182.  
  183. A word of caution: no check is made to ensure that only  one
  184. pattern  matches  the  case argument.  The first match found
  185. defines the set of commands to be executed.  In the  example
  186. below  the commands following the second  will never be exe-
  187. cuted.
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                              -4-
  203.  
  204.  
  205.           case $# in
  206.           )  ;;
  207.           )  ;;
  208.           esac
  209.  
  210.  
  211. Another example of the use of the case  construction  is  to
  212. distinguish  between  different  forms  of an argument.  The
  213. following example is a fragment of a cc command.
  214.  
  215.           for i
  216.           do case $i in
  217.           -[ocs])    ;;
  218.           -)   echo \'unknown flag $i\' ;;
  219.           .c)  /lib/c0 $i  ;;
  220.           )    echo \'unexpected argument $i\' ;;
  221.           esac
  222.           done
  223.  
  224.  
  225. To allow the same commands to be associated with  more  than
  226. one  pattern  the case command provides for alternative pat-
  227. terns separated by a .  For example,
  228.  
  229.           case $i in
  230.           -x-y)
  231.           esac
  232.  
  233. is equivalent to
  234.  
  235.           case $i in
  236.           -[xy])
  237.           esac
  238.  
  239.  
  240. The usual quoting conventions apply so that
  241.  
  242.           case $i in
  243.           \\?)
  244.  
  245. will match the character ?.
  246.  
  247. 2.3 Here documents
  248.  
  249. The shell  procedure  tel  in  section  2.1  uses  the  file
  250. /usr/lib/telnos to supply the data for grep.  An alternative
  251. is to include this data within the shell procedure as a here
  252. document, as in,
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                              -5-
  269.  
  270.  
  271.           for i
  272.           do grep $i !
  273.  
  274.           fred mh0123
  275.           bert mh0789
  276.  
  277.           !
  278.           done
  279.  
  280. In  this  example  the shell takes the lines between ! and !
  281. as the standard input for grep.  The string ! is  arbitrary,
  282. the document being terminated by a line that consists of the
  283. string following .
  284.  
  285. Parameters are substituted in the document before it is made
  286. available  to grep as illustrated by the following procedure
  287. called edg.
  288.  
  289.           ed $3 %
  290.           g/$1/s//$2/g
  291.           w
  292.           %
  293.  
  294. The call
  295.  
  296.           edg string1 string2 file
  297.  
  298. is then equivalent to the command
  299.  
  300.           ed file %
  301.           g/string1/s//string2/g
  302.           w
  303.           %
  304.  
  305. and changes all occurrences of string1 in file  to  string2.
  306. Substitution  can  be prevented using \ to quote the special
  307. character $ as in
  308.  
  309.           ed $3 +
  310.           1,\\$s/$1/$2/g
  311.           w
  312.           +
  313.  
  314. (This version of edg is equivalent to the first except  that
  315. ed  will print a ? if there are no occurrences of the string
  316. $1.)  Substitution within a here document may  be  prevented
  317. entirely by quoting the terminating string, for example,
  318.  
  319.           grep $i \\#
  320.  
  321.           #
  322.  
  323. The  document is presented without modification to grep.  If
  324. parameter substitution is not required in  a  here  document
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                              -6-
  335.  
  336.  
  337. this latter form is more efficient.
  338.  
  339. 2.4 Shell variables
  340.  
  341. The  shell provides string-valued variables.  Variable names
  342. begin with a letter  and  consist  of  letters,  digits  and
  343. underscores.   Variables may be given values by writing, for
  344. example,
  345.  
  346.           user=fred box=m000 acct=mh0000
  347.  
  348. which assigns values to the variables user, box and acct.  A
  349. variable  may be set to the null string by saying, for exam-
  350. ple,
  351.  
  352.           null=
  353.  
  354. The value of a variable is substituted by preceding its name
  355. with $; for example,
  356.  
  357.           echo $user
  358.  
  359. will echo fred.
  360.  
  361. Variables may be used interactively to provide abbreviations
  362. for frequently used strings.  For example,
  363.  
  364.           b=/usr/fred/bin
  365.           mv pgm $b
  366.  
  367. will move the file pgm from the  current  directory  to  the
  368. directory  /usr/fred/bin.  A more general notation is avail-
  369. able for parameter (or variable) substitution, as in,
  370.  
  371.           echo ${user}
  372.  
  373. which is equivalent to
  374.  
  375.           echo $user
  376.  
  377. and is used when the parameter name is followed by a  letter
  378. or digit.  For example,
  379.  
  380.           tmp=/tmp/ps
  381.           ps a >${tmp}a
  382.  
  383. will direct the output of ps to the file /tmp/psa, whereas,
  384.  
  385.           ps a >$tmpa
  386.  
  387. would  cause  the  value  of the variable tmpa to be substi-
  388. tuted.
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                              -7-
  401.  
  402.  
  403. Except for $? the following are set initially by the  shell.
  404. $? is set after executing each command.
  405.  
  406.      $?      The  exit status (return code) of the last com-
  407.              mand executed as a decimal string.   Most  com-
  408.              mands  return  a  zero exit status if they com-
  409.              plete successfully, otherwise a  non-zero  exit
  410.              status  is  returned.   Testing  the  value  of
  411.              return codes is dealt with later under  if  and
  412.              while commands.
  413.  
  414.      $#      The  number  of positional parameters (in deci-
  415.              mal).  Used, for example, in the append command
  416.              to check the number of parameters.
  417.  
  418.      $$      The  process number of this shell (in decimal).
  419.              Since process  numbers  are  unique  among  all
  420.              existing  processes,  this string is frequently
  421.              used to generate unique temporary  file  names.
  422.              For example,
  423.  
  424.                        ps a >/tmp/ps$$
  425.  
  426.                        rm /tmp/ps$$
  427.  
  428.  
  429.      $!      The  process  number of the last process run in
  430.              the background (in decimal).
  431.  
  432.      $-      The current shell flags, such as -x and -v.
  433.  
  434. Some variables have a  special  meaning  to  the  shell  and
  435. should be avoided for general use.
  436.  
  437.      $MAIL   When  used interactively the shell looks at the
  438.              file  specified  by  this  variable  before  it
  439.              issues  a  prompt.   If  the specified file has
  440.              been modified since it was last looked  at  the
  441.              shell  prints  the message you have mail before
  442.              prompting for the next command.  This  variable
  443.              is  typically  set in the file .profile, in the
  444.              user's login directory.  For example,
  445.  
  446.                        MAIL=/usr/spool/mail/fred
  447.  
  448.  
  449.      $HOME   The default argument for the cd  command.   The
  450.              current  directory is used to resolve file name
  451.              references that do not begin with a /,  and  is
  452.              changed using the cd command.  For example,
  453.  
  454.                        cd /usr/fred/bin
  455.  
  456.              makes the current directory /usr/fred/bin.
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                              -8-
  467.  
  468.  
  469.                        cat wn
  470.  
  471.              will  print on the terminal the file wn in this
  472.              directory.  The command cd with no argument  is
  473.              equivalent to
  474.  
  475.                        cd $HOME
  476.  
  477.              This  variable is also typically set in the the
  478.              user's login profile.
  479.  
  480.      $PATH   A list of  directories  that  contain  commands
  481.              (the search path).  Each time a command is exe-
  482.              cuted by the shell a  list  of  directories  is
  483.              searched  for  an executable file.  If $PATH is
  484.              not set then the current directory,  /bin,  and
  485.              /usr/bin  are  searched  by default.  Otherwise
  486.              $PATH consists of directory names separated  by
  487.              :.  For example,
  488.  
  489.                        PATH=:/usr/fred/bin:/bin:/usr/bin
  490.  
  491.              specifies  that the current directory (the null
  492.              string before the first :), /usr/fred/bin, /bin
  493.              and  /usr/bin are to be searched in that order.
  494.              In this way individual users can have their own
  495.              `private' commands that are accessible indepen-
  496.              dently of the current directory.  If  the  com-
  497.              mand  name  contains  a  /  then this directory
  498.              search is not used; a single attempt is made to
  499.              execute the command.
  500.  
  501.      $PS1    The  primary  shell  prompt string, by default,
  502.              `$ '.
  503.  
  504.      $PS2    The shell prompt when further input is  needed,
  505.              by default, `> '.
  506.  
  507.      $IFS    The set of characters used by blank interpreta-
  508.              tion (see section 3.4).
  509.  
  510. 2.5 The test command
  511.  
  512. The test  command,  although  not  part  of  the  shell,  is
  513. intended for use by shell programs.  For example,
  514.  
  515.           test -f file
  516.  
  517. returns  zero  exit  status if file exists and non-zero exit
  518. status otherwise.  In general test evaluates a predicate and
  519. returns  the  result  as  its exit status.  Some of the more
  520. frequently used test arguments are given here, see test  (1)
  521. for a complete specification.
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                              -9-
  533.  
  534.  
  535.           test s         true if the argument s is not the null string
  536.           test -f file   true if file exists
  537.           test -r file   true if file is readable
  538.           test -w file   true if file is writable
  539.           test -d file   true if file is a directory
  540.  
  541.  
  542. 2.6 Control flow - while
  543.  
  544. The  actions  of the for loop and the case branch are deter-
  545. mined by data available to the shell.  A while or until loop
  546. and  an  if then else branch are also provided whose actions
  547. are determined by the exit status returned by  commands.   A
  548. while loop has the general form
  549.  
  550.           while command-list
  551.           do command-list
  552.           done
  553.  
  554.  
  555. The  value tested by the while command is the exit status of
  556. the last simple command following while.   Each  time  round
  557. the  loop command-list is executed; if a zero exit status is
  558. returned then command-list is executed; otherwise, the  loop
  559. terminates.  For example,
  560.  
  561.           while test $1
  562.           do
  563.           shift
  564.           done
  565.  
  566. is equivalent to
  567.  
  568.           for i
  569.           do
  570.           done
  571.  
  572. shift is a shell command that renames the positional parame-
  573. ters $2, $3,  as $1, $2, and loses $1.
  574.  
  575. Another kind of use for the  while/until  loop  is  to  wait
  576. until some external event occurs and then run some commands.
  577. In an until loop the termination condition is reversed.  For
  578. example,
  579.  
  580.           until test -f file
  581.           do sleep 300; done
  582.           commands
  583.  
  584. will  loop  until  file exists.  Each time round the loop it
  585. waits  for  5  minutes  before  trying  again.   (Presumably
  586. another process will eventually create the file.)
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                             -10-
  599.  
  600.  
  601. 2.7 Control flow - if
  602.  
  603. Also available is a general conditional branch of the form,
  604.  
  605.           if command-list
  606.           then command-list
  607.           else command-list
  608.           fi
  609.  
  610. that  tests  the  value  returned by the last simple command
  611. following if.
  612.  
  613. The if command may be used in conjunction with the test com-
  614. mand to test for the existence of a file as in
  615.  
  616.           if test -f file
  617.           then process file
  618.           else do something else
  619.           fi
  620.  
  621.  
  622. An  example  of the use of if, case and for constructions is
  623. given in section 2.10.
  624.  
  625. A multiple test if command of the form
  626.  
  627.           if
  628.           then
  629.           else if
  630.                then
  631.                else if
  632.  
  633.                     fi
  634.                fi
  635.           fi
  636.  
  637. may be written using an extension of the if notation as,
  638.  
  639.           if
  640.           then
  641.           elif
  642.           then
  643.           elif
  644.  
  645.           fi
  646.  
  647.  
  648. The following example is the touch command which changes the
  649. `last  modified'  time for a list of files.  The command may
  650. be used in conjunction with make (1) to force  recompilation
  651. of a list of files.
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                             -11-
  665.  
  666.  
  667.           flag=
  668.           for i
  669.           do case $i in
  670.           -c)  flag=N ;;
  671.           )    if test -f $i
  672.                then ln $i junk$$; rm junk$$
  673.                elif test $flag
  674.                then echo file \\'$i\\' does not exist
  675.                else >$i
  676.                fi
  677.            esac
  678.           done
  679.  
  680. The  -c  flag  is  used  in this command to force subsequent
  681. files to be created if they do not  already  exist.   Other-
  682. wise,  if  the  file  does  not  exist,  an error message is
  683. printed.  The shell variable flag is set  to  some  non-null
  684. string if the -c argument is encountered.  The commands
  685.  
  686.           ln ; rm
  687.  
  688. make  a link to the file and then remove it thus causing the
  689. last modified date to be updated.
  690.  
  691. The sequence
  692.  
  693.           if command1
  694.           then command2
  695.           fi
  696.  
  697. may be written
  698.  
  699.           command1 && command2
  700.  
  701. Conversely,
  702.  
  703.           command1  command2
  704.  
  705. executes command2 only if command1 fails.  In each case  the
  706. value  returned is that of the last simple command executed.
  707.  
  708. 2.8 Command grouping
  709.  
  710. Commands may be grouped in two ways,
  711.  
  712.           { command-list ; }
  713.  
  714. and
  715.  
  716.           ( command-list )
  717.  
  718.  
  719. In the first command-list is simply  executed.   The  second
  720. form  executes  command-list  as  a  separate  process.  For
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                             -12-
  731.  
  732.  
  733. example,
  734.  
  735.           (cd x; rm junk )
  736.  
  737. executes rm junk in the directory  x  without  changing  the
  738. current directory of the invoking shell.
  739.  
  740. The commands
  741.  
  742.           cd x; rm junk
  743.  
  744. have  the  same  effect  but leave the invoking shell in the
  745. directory x.
  746.  
  747. 2.9 Debugging shell procedures
  748.  
  749. The shell provides  two  tracing  mechanisms  to  help  when
  750. debugging shell procedures.  The first is invoked within the
  751. procedure as
  752.  
  753.           set -v
  754.  
  755. (v for verbose) and causes lines  of  the  procedure  to  be
  756. printed as they are read.  It is useful to help isolate syn-
  757. tax errors.  It may be invoked without modifying the  proce-
  758. dure by saying
  759.  
  760.           sh -v proc
  761.  
  762. where  proc  is  the name of the shell procedure.  This flag
  763. may be used in conjunction with the -n flag  which  prevents
  764. execution  of subsequent commands.  (Note that saying set -n
  765. at a terminal will render the terminal useless until an end-
  766. of-file is typed.)
  767.  
  768. The command
  769.  
  770.           set -x
  771.  
  772. will  produce  an execution trace.  Following parameter sub-
  773. stitution each command is printed as it is  executed.   (Try
  774. these  at  the terminal to see what effect they have.)  Both
  775. flags may be turned off by saying
  776.  
  777.           set -
  778.  
  779. and the current setting of the shell flags is  available  as
  780. $-.
  781.  
  782. 2.10 The man command
  783.  
  784. The  following  is  the  man command which is used to diplay
  785. sections of the UNIX manual on your terminal.  It is called,
  786. for example, as
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                             -13-
  797.  
  798.  
  799.                man sh
  800.                man -t ed
  801.                man 2 fork
  802.  
  803. In the first the manual section for sh is displayed..  Since
  804. no section is specified, section  1  is  used.   The  second
  805. example  will typeset (-t option) the manual section for ed.
  806. The last prints the fork manual page from section  2,  which
  807. covers system calls.
  808.  
  809.  
  810.      found=yes
  811.                     fi
  812.                     done
  813.                     case $found in
  814.                     no) echo \'$i: manual page not found\'
  815.                     esac
  816.                fi
  817.           esac
  818.           done
  819.            Figure 1. A version of the man command
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.