home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1997 December / Internet_Info_CD-ROM_Walnut_Creek_December_1997.iso / infoguide / advanced / unix / unix-faq-part3 < prev    next >
Text File  |  1997-12-01  |  31KB  |  741 lines

  1. Archive-name: unix-faq/faq/part3
  2. Version: $Id: part3,v 2.4 1994/01/14 14:58:06 tmatimar Exp $
  3.  
  4. These seven articles contain the answers to some Frequently Asked
  5. Questions often seen in comp.unix.questions and comp.unix.shell.
  6. Please don't ask these questions again, they've been answered plenty
  7. of times already - and please don't flame someone just because they may
  8. not have read this particular posting.  Thank you.
  9.  
  10. This collection of documents is Copyright (c) 1994, Ted Timar.
  11. All rights reserved.  Permission to distribute the collection is
  12. hereby granted providing that distribution is electronic, no money
  13. is involved, reasonable attempts are made to use the latest version
  14. and all credits and this copyright notice are maintained.
  15. Other requests for distribution will be considered.
  16.  
  17. All information here has been contributed with good intentions, but
  18. none of it is guaranteed either by the contributors or myself to be
  19. accurate.  The users of this information take all responsibility for
  20. any damage that may occur.
  21.  
  22. Many FAQs, including this one, are available on the archive site
  23. rtfm.mit.edu in the directory pub/usenet/news.answers.
  24. The name under which a FAQ is archived appears in the "Archive-Name:"
  25. line at the top of the article.  This FAQ is archived as
  26. "unix-faq/faq/part[1-7]".
  27.  
  28. These articles are divided approximately as follows:
  29.  
  30.       1.*) General questions.
  31.       2.*) Relatively basic questions, likely to be asked by beginners.
  32.       3.*) Intermediate questions.
  33.       4.*) Advanced questions, likely to be asked by people who thought
  34.        they already knew all of the answers.
  35.       5.*) Questions pertaining to the various shells, and the differences.
  36.       6.*) An overview of Unix variants.
  37.       7.*) An comparison of configuration management systems (RCS, SCCS).
  38.  
  39. This article includes answers to:
  40.  
  41.       3.1)  How do I find the creation time of a file?
  42.       3.2)  How do I use "rsh" without having the rsh hang around
  43.               until the remote command has completed?
  44.       3.3)  How do I truncate a file?
  45.       3.4)  Why doesn't find's "{}" symbol do what I want?
  46.       3.5)  How do I set the permissions on a symbolic link?
  47.       3.6)  How do I "undelete" a file?
  48.       3.7)  How can a process detect if it's running in the background?
  49.       3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  50.       3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  51.               programs from a shell script or in the background?
  52.       3.10) How do I find the process ID of a program with a particular
  53.             name from inside a shell script or C program?
  54.       3.11) How do I check the exit status of a remote command
  55.             executed via "rsh" ?
  56.       3.12) Is it possible to pass shell variable settings into an awk program?
  57.       3.13) How do I get rid of zombie processes that persevere?
  58.       3.14) How do I get lines from a pipe as they are written instead of
  59.             only in larger blocks.
  60.  
  61. If you're looking for the answer to, say, question 3.5, and want to skip
  62. everything else, you can search ahead for the regular expression "^3.5)".
  63.  
  64. While these are all legitimate questions, they seem to crop up in
  65. comp.unix.questions or comp.unix.shell on an annual basis, usually
  66. followed by plenty of replies (only some of which are correct) and then
  67. a period of griping about how the same questions keep coming up.  You
  68. may also like to read the monthly article "Answers to Frequently Asked
  69. Questions" in the newsgroup "news.announce.newusers", which will tell
  70. you what "UNIX" stands for.
  71.  
  72. With the variety of Unix systems in the world, it's hard to guarantee
  73. that these answers will work everywhere.  Read your local manual pages
  74. before trying anything suggested here.  If you have suggestions or
  75. corrections for any of these answers, please send them to to
  76. tmatimar@empress.com.
  77.  
  78. ----------------------------------------------------------------------
  79.  
  80. Subject: How do I find the creation time of a file?
  81. Date: Thu Mar 18 17:16:55 EST 1993
  82.  
  83. 3.1)  How do I find the creation time of a file?
  84.  
  85.       You can't - it isn't stored anywhere.  Files have a last-modified
  86.       time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
  87.       and an inode change time (shown by "ls -lc"). The latter is often
  88.       referred to as the "creation time" - even in some man pages -
  89.       but that's wrong; it's also set by such operations as mv, ln,
  90.       chmod, chown and chgrp.
  91.  
  92.       The man page for "stat(2)" discusses this.
  93.  
  94. ------------------------------
  95.  
  96. Subject: How do I use "rsh" without having the rsh hang around ... ?
  97. Date: Thu Mar 18 17:16:55 EST 1993
  98.  
  99. 3.2)  How do I use "rsh" without having the rsh hang around until the
  100.       remote command has completed?
  101.  
  102.       (See note in question 2.7 about what "rsh" we're talking about.)
  103.  
  104.       The obvious answers fail:
  105.               rsh machine command &
  106.       or      rsh machine 'command &'
  107.  
  108.       For instance, try doing   rsh machine 'sleep 60 &' and you'll see
  109.       that the 'rsh' won't exit right away.  It will wait 60 seconds
  110.       until the remote 'sleep' command finishes, even though that
  111.       command was started in the background on the remote machine.  So
  112.       how do you get the 'rsh' to exit immediately after the 'sleep' is
  113.       started?
  114.  
  115.       The solution - if you use csh on the remote machine:
  116.  
  117.         rsh machine -n 'command >&/dev/null </dev/null &'
  118.  
  119.       If you use sh on the remote machine:
  120.  
  121.         rsh machine -n 'command >/dev/null 2>&1 </dev/null &'
  122.  
  123.       Why?  "-n" attaches rsh's stdin to /dev/null so you could run the
  124.       complete rsh command in the background on the LOCAL machine.
  125.       Thus "-n" is equivalent to another specific "< /dev/null".
  126.       Furthermore, the input/output redirections on the REMOTE machine
  127.       (inside the single quotes) ensure that rsh thinks the session can
  128.       be terminated (there's no data flow any more.)
  129.  
  130.       Note: The file that you redirect to/from on the remote machine
  131.       doesn't have to be /dev/null; any ordinary file will do.
  132.  
  133.       In many cases, various parts of these complicated commands
  134.       aren't necessary.
  135.  
  136. ------------------------------
  137.  
  138. Subject: How do I truncate a file?
  139. Date: Thu Mar 18 17:16:55 EST 1993
  140.  
  141. 3.3)  How do I truncate a file?
  142.  
  143.       The BSD function ftruncate() sets the length of a file.  Xenix -
  144.       and therefore SysV r3.2 and later - has the chsize() system
  145.       call.  For other systems, the only kind of truncation you can do
  146.       is truncation to length zero with creat() or open(..., O_TRUNC).
  147.  
  148. ------------------------------
  149.  
  150. Subject: Why doesn't find's "{}" symbol do what I want?
  151. Date: Thu Mar 18 17:16:55 EST 1993
  152.  
  153. 3.4)  Why doesn't find's "{}" symbol do what I want?
  154.  
  155.       "find" has a -exec option that will execute a particular command
  156.       on all the selected files. Find will replace any "{}" it sees
  157.       with the name of the file currently under consideration.
  158.  
  159.       So, some day you might try to use "find" to run a command on
  160.       every file, one directory at a time.  You might try this:
  161.  
  162.     find /path -type d -exec command {}/\* \;
  163.  
  164.       hoping that find will execute, in turn
  165.  
  166.     command directory1/*
  167.     command directory2/*
  168.     ...
  169.  
  170.       Unfortunately, find only expands the "{}" token when it appears
  171.       by itself.  Find will leave anything else like "{}/*" alone, so
  172.       instead of doing what you want, it will do
  173.  
  174.     command {}/*
  175.     command {}/*
  176.     ...
  177.  
  178.       once for each directory.  This might be a bug, it might be a
  179.       feature, but we're stuck with the current behaviour.
  180.  
  181.       So how do you get around this?  One way would be to write a
  182.       trivial little shell script, let's say "./doit", that consists of
  183.  
  184.     command "$1"/*
  185.  
  186.       You could then use
  187.  
  188.     find /path -type d -exec ./doit {} \;
  189.  
  190.       Or if you want to avoid the "./doit" shell script, you can use
  191.  
  192.     find /path -type d -exec sh -c 'command $0/*' {} \;
  193.  
  194.       (This works because within the 'command' of "sh -c 'command' A B C ...",
  195.        $0 expands to A, $1 to B, and so on.)
  196.  
  197.       or you can use the construct-a-command-with-sed trick
  198.  
  199.     find /path -type d -print | sed 's:.*:command &/*:' | sh
  200.  
  201.       If all you're trying to do is cut down on the number of times
  202.       that "command" is executed, you should see if your system has the
  203.       "xargs" command.  Xargs reads arguments one line at a time from
  204.       the standard input and assembles as many of them as will fit into
  205.       one command line.  You could use
  206.  
  207.     find /path -print | xargs command
  208.  
  209.       which would result in one or more executions of
  210.  
  211.     command file1 file2 file3 file4 dir1/file1 dir1/file2
  212.  
  213.       Unfortunately this is not a perfectly robust or secure solution.
  214.       Xargs expects its input lines to be terminated with newlines, so
  215.       it will be confused by files with odd characters such as newlines
  216.       in their names.
  217.  
  218. ------------------------------
  219.  
  220. Subject: How do I set the permissions on a symbolic link?
  221. Date: Thu Mar 18 17:16:55 EST 1993
  222.  
  223. 3.5)  How do I set the permissions on a symbolic link?
  224.  
  225.       Permissions on a symbolic link don't really mean anything.  The
  226.       only permissions that count are the permissions on the file that
  227.       the link points to.
  228.  
  229. ------------------------------
  230.  
  231. Subject: How do I "undelete" a file?
  232. Date: Thu Mar 18 17:16:55 EST 1993
  233.  
  234. 3.6)  How do I "undelete" a file?
  235.  
  236.       Someday, you are going to accidentally type something like
  237.       "rm * .foo", and find you just deleted "*" instead of "*.foo".
  238.       Consider it a rite of passage.
  239.  
  240.       Of course, any decent systems administrator should be doing
  241.       regular backups.  Check with your sysadmin to see if a recent
  242.       backup copy of your file is available.  But if it isn't, read
  243.       on.
  244.  
  245.       For all intents and purposes, when you delete a file with "rm" it
  246.       is gone.  Once you "rm" a file, the system totally forgets which
  247.       blocks scattered around the disk comprised your file.  Even
  248.       worse, the blocks from the file you just deleted are going to be
  249.       the first ones taken and scribbled upon when the system needs
  250.       more disk space.  However, never say never.  It is theoretically
  251.       possible *if* you shut down the system immediately after the "rm"
  252.       to recover portions of the data.  However, you had better have a
  253.       very wizardly type person at hand with hours or days to spare to
  254.       get it all back.
  255.  
  256.       Your first reaction when you "rm" a file by mistake is why not
  257.       make a shell alias or procedure which changes "rm" to move files
  258.       into a trash bin rather than delete them?  That way you can
  259.       recover them if you make a mistake, and periodically clean out
  260.       your trash bin.  Two points:  first, this is generally accepted
  261.       as a *bad* idea.  You will become dependent upon this behaviour
  262.       of "rm", and you will find yourself someday on a normal system
  263.       where "rm" is really "rm", and you will get yourself in trouble.
  264.       Second, you will eventually find that the hassle of dealing with
  265.       the disk space and time involved in maintaining the trash bin, it
  266.       might be easier just to be a bit more careful with "rm".  For
  267.       starters, you should look up the "-i" option to "rm" in your
  268.       manual.
  269.  
  270.       If you are still undaunted, then here is a possible simple
  271.       answer.  You can create yourself a "can" command which moves
  272.       files into a trashcan directory. In csh(1) you can place the
  273.       following commands in the ".login" file in your home directory:
  274.  
  275.     alias can    'mv \!* ~/.trashcan'       # junk file(s) to trashcan
  276.     alias mtcan    'rm -f ~/.trashcan/*'       # irretrievably empty trash
  277.     if ( ! -d ~/.trashcan ) mkdir ~/.trashcan  # ensure trashcan exists
  278.  
  279.       You might also want to put a:
  280.  
  281.     rm -f ~/.trashcan/*
  282.  
  283.       in the ".logout" file in your home directory to automatically
  284.       empty the trash when you log out.  (sh and ksh versions are left
  285.       as an exercise for the reader.)
  286.  
  287.       MIT's Project Athena has produced a comprehensive
  288.       delete/undelete/expunge/purge package, which can serve as a
  289.       complete replacement for rm which allows file recovery.  This
  290.       package was posted to comp.sources.misc (volume 17, issue
  291.       023-026)
  292.  
  293. ------------------------------
  294.  
  295. Subject: How can a process detect if it's running in the background?
  296. Date: Thu Mar 18 17:16:55 EST 1993
  297.  
  298. 3.7)  How can a process detect if it's running in the background?
  299.  
  300.       First of all: do you want to know if you're running in the
  301.       background, or if you're running interactively? If you're
  302.       deciding whether or not you should print prompts and the like,
  303.       that's probably a better criterion. Check if standard input
  304.       is a terminal:
  305.  
  306.         sh: if [ -t 0 ]; then ... fi
  307.         C: if(isatty(0)) { ... }
  308.  
  309.       In general, you can't tell if you're running in the background.
  310.       The fundamental problem is that different shells and different
  311.       versions of UNIX have different notions of what "foreground" and
  312.       "background" mean - and on the most common type of system with a
  313.       better-defined notion of what they mean, programs can be moved
  314.       arbitrarily between foreground and background!
  315.  
  316.       UNIX systems without job control typically put a process into the
  317.       background by ignoring SIGINT and SIGQUIT and redirecting the
  318.       standard input to "/dev/null"; this is done by the shell.
  319.  
  320.       Shells that support job control, on UNIX systems that support job
  321.       control, put a process into the background by giving it a process
  322.       group ID different from the process group to which the terminal
  323.       belongs.  They move it back into the foreground by setting the
  324.       terminal's process group ID to that of the process.  Shells that
  325.       do *not* support job control, on UNIX systems that support job
  326.       control, typically do what shells do on systems that don't
  327.       support job control.
  328.  
  329. ------------------------------
  330.  
  331. Subject: Why doesn't redirecting a loop work as intended?  (Bourne shell)
  332. Date: Thu Mar 18 17:16:55 EST 1993
  333.  
  334. 3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  335.  
  336.       Take the following example:
  337.  
  338.     foo=bar
  339.  
  340.     while read line
  341.     do
  342.         # do something with $line
  343.         foo=bletch
  344.     done < /etc/passwd
  345.  
  346.     echo "foo is now: $foo"
  347.  
  348.       Despite the assignment ``foo=bletch'' this will print
  349.       ``foo is now: bar'' in many implementations of the Bourne shell.
  350.       Why?  Because of the following, often undocumented, feature of
  351.       historic Bourne shells: redirecting a control structure (such as
  352.       a loop, or an ``if'' statement) causes a subshell to be created,
  353.       in which the structure is executed; variables set in that
  354.       subshell (like the ``foo=bletch'' assignment) don't affect the
  355.       current shell, of course.
  356.  
  357.       The POSIX 1003.2 Shell and Tools Interface standardization
  358.       committee forbids the behaviour described above, i.e. in P1003.2
  359.       conformant Bourne shells the example will print ``foo is now:
  360.       bletch''.
  361.  
  362.       In historic (and P1003.2 conformant) implementations you can use
  363.       the following `trick' to get around the redirection problem:
  364.  
  365.     foo=bar
  366.  
  367.     # make file descriptor 9 a duplicate of file descriptor 0 (stdin);
  368.     # then connect stdin to /etc/passwd; the original stdin is now
  369.     # `remembered' in file descriptor 9; see dup(2) and sh(1)
  370.     exec 9<&0 < /etc/passwd
  371.  
  372.     while read line
  373.     do
  374.         # do something with $line
  375.         foo=bletch
  376.     done
  377.  
  378.     # make stdin a duplicate of file descriptor 9, i.e. reconnect
  379.     # it to the original stdin; then close file descriptor 9
  380.     exec 0<&9 9<&-
  381.  
  382.     echo "foo is now: $foo"
  383.  
  384.       This should always print ``foo is now: bletch''.
  385.       Right, take the next example:
  386.  
  387.     foo=bar
  388.  
  389.     echo bletch | read foo
  390.  
  391.     echo "foo is now: $foo"
  392.  
  393.       This will print ``foo is now: bar'' in many implementations,
  394.       ``foo is now: bletch'' in some others.  Why?  Generally each part
  395.       of a pipeline is run in a different subshell; in some
  396.       implementations though, the last command in the pipeline is made
  397.       an exception: if it is a builtin command like ``read'', the
  398.       current shell will execute it, else another subshell is created.
  399.  
  400.       POSIX 1003.2 allows both behaviours so portable scripts cannot
  401.       depend on any of them.
  402.  
  403. ------------------------------
  404.  
  405. Subject: How do I run ... interactive programs from a shell script ... ?
  406. Date: Thu Mar 18 17:16:55 EST 1993
  407.  
  408. 3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  409.       programs from a shell script or in the background?
  410.  
  411.       These programs expect a terminal interface.  Shells makes no
  412.       special provisions to provide one.  Hence, such programs cannot
  413.       be automated in shell scripts.
  414.  
  415.       The 'expect' program provides a programmable terminal interface
  416.       for automating interaction with such programs.  The following
  417.       expect script is an example of a non-interactive version of
  418.       passwd(1).
  419.  
  420.     # username is passed as 1st arg, password as 2nd
  421.     set password [index $argv 2]
  422.     spawn passwd [index $argv 1]
  423.     expect "*password:"
  424.     send "$password\r"
  425.     expect "*password:"
  426.     send "$password\r"
  427.     expect eof
  428.  
  429.       expect can partially automate interaction which is especially
  430.       useful for telnet, rlogin, debuggers or other programs that have
  431.       no built-in command language.  The distribution provides an
  432.       example script to rerun rogue until a good starting configuration
  433.       appears.  Then, control is given back to the user to enjoy the game.
  434.  
  435.       Fortunately some programs have been written to manage the
  436.       connection to a pseudo-tty so that you can run these sorts of
  437.       programs in a script.
  438.  
  439.       To get expect, email "send pub/expect/expect.shar.Z" to
  440.       library@cme.nist.gov or anonymous ftp same from
  441.       ftp.cme.nist.gov.
  442.  
  443.       Another solution is provided by the pty 4.0 program, which runs a
  444.       program under a pseudo-tty session and was posted to
  445.       comp.sources.unix, volume 25.  A pty-based solution using named
  446.       pipes to do the same as the above might look like this:
  447.  
  448.     #!/bin/sh
  449.     /etc/mknod out.$$ p; exec 2>&1
  450.     ( exec 4<out.$$; rm -f out.$$
  451.     <&4 waitfor 'password:'
  452.         echo "$2"
  453.     <&4 waitfor 'password:'
  454.         echo "$2"
  455.     <&4 cat >/dev/null
  456.     ) | ( pty passwd "$1" >out.$$ )
  457.  
  458.       Here, 'waitfor' is a simple C program that searches for
  459.       its argument in the input, character by character.
  460.  
  461.       A simpler pty solution (which has the drawback of not
  462.       synchronizing properly with the passwd program) is
  463.  
  464.     #!/bin/sh
  465.     ( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"
  466.  
  467. ------------------------------
  468.  
  469. Subject: How do I find the process ID of a program with a particular name ... ?
  470. Date: Thu Mar 18 17:16:55 EST 1993
  471.  
  472. 3.10) How do I find the process ID of a program with a particular name
  473.       from inside a shell script or C program?
  474.  
  475.       In a shell script:
  476.  
  477.       There is no utility specifically designed to map between program
  478.       names and process IDs.  Furthermore, such mappings are often
  479.       unreliable, since it's possible for more than one process to have
  480.       the same name, and since it's possible for a process to change
  481.       its name once it starts running.  However, a pipeline like this
  482.       can often be used to get a list of processes (owned by you) with
  483.       a particular name:
  484.  
  485.         ps ux | awk '/name/ && !/awk/ {print $2}'
  486.  
  487.       You replace "name" with the name of the process for which you are
  488.       searching.
  489.  
  490.       The general idea is to parse the output of ps, using awk or grep
  491.       or other utilities, to search for the lines with the specified
  492.       name on them, and print the PID's for those lines.  Note that the
  493.       "!/awk/" above prevents the awk process for being listed.
  494.  
  495.       You may have to change the arguments to ps, depending on what
  496.       kind of Unix you are using.
  497.  
  498.       In a C program:
  499.  
  500.       Just as there is no utility specifically designed to map between
  501.       program names and process IDs, there are no (portable) C library
  502.       functions to do it either.
  503.  
  504.       However, some vendors provide functions for reading Kernel
  505.       memory; for example, Sun provides the "kvm_" functions, and Data
  506.       General provides the "dg_" functions.  It may be possible for any
  507.       user to use these, or they may only be useable by the super-user
  508.       (or a user in group "kmem") if read-access to kernel memory on
  509.       your system is restricted.  Furthermore, these functions are
  510.       often not documented or documented badly, and might change from
  511.       release to release.
  512.  
  513.       Some vendors provide a "/proc" filesystem, which appears as a
  514.       directory with a bunch of filenames in it.  Each filename is a
  515.       number, corresponding to a process ID, and you can open the file
  516.       and read it to get information about the process.  Once again,
  517.       access to this may be restricted, and the interface to it may
  518.       change from system to system.
  519.  
  520.       If you can't use vendor-specific library functions, and you
  521.       don't have /proc, and you still want to do this completely
  522.       in C, you
  523.       are going to have to do the grovelling through kernel memory
  524.       yourself.  For a good example of how to do this on many systems,
  525.       see the sources to "ofiles", available in the comp.sources.unix
  526.       archives.  (A package named "kstuff" to help with kernel
  527.       grovelling was posted to alt.sources in May 1991 and is also
  528.       available via anonymous ftp as
  529.       usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  530.       wuarchive.wustl.edu.)
  531.  
  532. ------------------------------
  533.  
  534. Subject: How do I check the exit status of a remote command executed via "rsh"?
  535. Date: Thu Mar 18 17:16:55 EST 1993
  536.  
  537. 3.11) How do I check the exit status of a remote command
  538.       executed via "rsh" ?
  539.  
  540.       This doesn't work:
  541.  
  542.     rsh some-machine some-crummy-command || echo "Command failed"
  543.  
  544.       The exit status of 'rsh' is 0 (success) if the rsh program
  545.       itself completed successfully, which probably isn't what
  546.       you wanted.
  547.  
  548.       If you want to check on the exit status of the remote program,
  549.       you can try using Maarten Litmaath's 'ersh' script, which was
  550.       posted to alt.sources in January, 1991.  ersh is a shell script
  551.       that calls rsh, arranges for the remote machine to echo the
  552.       status of the command after it completes, and exits with that
  553.       status.
  554.  
  555. ------------------------------
  556.  
  557. Subject: Is it possible to pass shell variable settings into an awk program?
  558. Date: Thu Mar 18 17:16:55 EST 1993
  559.  
  560. 3.12) Is it possible to pass shell variable settings into an awk program?
  561.  
  562.       There are two different ways to do this.  The first involves
  563.       simply expanding the variable where it is needed in the program.
  564.       For example, to get a list of all ttys you're using:
  565.  
  566.     who | awk '/^'"$USER"'/ { print $2 }'                (1)
  567.  
  568.       Single quotes are usually used to enclose awk programs because
  569.       the character '$' is often used in them, and '$' will be
  570.       interpreted by the shell if enclosed inside double quotes, but
  571.       not if enclosed inside single quotes.  In this case, we *want*
  572.       the '$' in "$USER" to be interpreted by the shell, so we close
  573.       the single quotes and then put the "$USER" inside double quotes.
  574.       Note that there are no spaces in any of that, so the shell will
  575.       see it all as one argument.  Note, further, that the double
  576.       quotes probably aren't necessary in this particular case (i.e. we
  577.       could have done
  578.  
  579.     who | awk '/^'$USER'/ { print $2 }'                (2)
  580.  
  581.       ), but they should be included nevertheless because they are
  582.       necessary when the shell variable in question contains special
  583.       characters or spaces.
  584.  
  585.       The second way to pass variable settings into awk is to use an
  586.       often undocumented feature of awk which allows variable settings
  587.       to be specified as "fake file names" on the command line.  For
  588.       example:
  589.  
  590.     who | awk '$1 == user { print $2 }' user="$USER" -        (3)
  591.  
  592.       Variable settings take effect when they are encountered on the
  593.       command line, so, for example, you could instruct awk on how to
  594.       behave for different files using this technique.  For example:
  595.  
  596.     awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  597.  
  598.       Note that some versions of awk will cause variable settings
  599.       encountered before any real filenames to take effect before the
  600.       BEGIN block is executed, but some won't so neither way should be
  601.       relied upon.
  602.  
  603.       Note, further, that when you specify a variable setting, awk
  604.       won't automatically read from stdin if no real files are
  605.       specified, so you need to add a "-" argument to the end of your
  606.       command, as I did at (3) above.
  607.  
  608. ------------------------------
  609.  
  610. Subject: How do I get rid of zombie processes that persevere?
  611. >From: jik@rtfm.MIT.Edu (Jonathan I. Kamens)
  612. >From: casper@fwi.uva.nl (Casper Dik)
  613. Date: Thu, 09 Sep 93 16:39:58 +0200
  614.  
  615. 3.13) How do I get rid of zombie processes that persevere?
  616.  
  617.       Unfortunately, it's impossible to generalize how the death of
  618.       child processes should behave, because the exact mechanism varies
  619.       over the various flavors of Unix.
  620.  
  621.       First of all, by default, you have to do a wait() for child
  622.       processes under ALL flavors of Unix.  That is, there is no flavor
  623.       of Unix that I know of that will automatically flush child
  624.       processes that exit, even if you don't do anything to tell it to
  625.       do so.
  626.  
  627.       Second, under some SysV-derived systems, if you do
  628.       "signal(SIGCHLD, SIG_IGN)" (well, actually, it may be SIGCLD
  629.       instead of SIGCHLD, but most of the newer SysV systems have
  630.       "#define SIGCHLD SIGCLD" in the header files), then child
  631.       processes will be cleaned up automatically, with no further
  632.       effort in your part.  The best way to find out if it works at
  633.       your site is to try it, although if you are trying to write
  634.       portable code, it's a bad idea to rely on this in any case.
  635.       Unfortunately, POSIX doesn't allow you to do this; the behavior
  636.       of setting the SIGCHLD to SIG_IGN under POSIX is undefined, so
  637.       you can't do it if your program is supposed to be
  638.       POSIX-compliant.
  639.  
  640.       So, what's the POSIX way? As mentioned earlier, you must
  641.       install a signal handler and wait. Under POSIX signal handlers
  642.       are installed with sigaction. Since you are not interested in
  643.       ``stopped'' children, only in terminated children, add SA_NOCLDSTOP
  644.       to sa_flags.  Waiting without blocking is done with waitpid().
  645.       The first argument to waitpid should be -1 (wait for any pid),
  646.       the third should be WNOHANG. This is the most portable way
  647.       and is likely to become more portable in future.
  648.  
  649.       If your systems doesn't support POSIX, there's a number of ways.
  650.       The easiest way is signal(SIGCHLD, SIG_IGN), if it works.
  651.       If SIG_IGN cannot be used to force automatic clean-up, then you've
  652.       got to write a signal handler to do it.  It isn't easy at all to
  653.       write a signal handler that does things right on all flavors of
  654.       Unix, because of the following inconsistencies:
  655.  
  656.       On some flavors of Unix, the SIGCHLD signal handler is called if
  657.       one *or more* children have died.  This means that if your signal
  658.       handler only does one wait() call, then it won't clean up all of
  659.       the children.  Fortunately, I believe that all Unix flavors for
  660.       which this is the case have available to the programmer the
  661.       wait3() or waitpid() call, which allows the WNOHANG option to
  662.       check whether or not there are any children waiting to be cleaned
  663.       up.  Therefore, on any system that has wait3()/waitpid(), your
  664.       signal handler should call wait3()/waitpid() over and over again
  665.       with the WNOHANG option until there are no children left to clean
  666.       up. Waitpid() is the preferred interface, as it is in POSIX.
  667.  
  668.       On SysV-derived systems, SIGCHLD signals are regenerated if there
  669.       are child processes still waiting to be cleaned up after you exit
  670.       the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  671.       systems to assume when the signal handler gets called that you
  672.       only have to clean up one signal, and assume that the handler
  673.       will get called again if there are more to clean up after it
  674.       exits.
  675.  
  676.       On older systems, there is no way to prevent signal handlers
  677.       from being automatically reset to SIG_DFL when the signal
  678.       handler gets called.  On such systems, you have to put
  679.       "signal(SIGCHILD, catcher_func)" (where "catcher_func" is the
  680.       name of the handler function) as the last thing in the signal
  681.       handler, so that it gets reset.
  682.  
  683.       Fortunately, newer implementations allow signal handlers to be
  684.       installed without being reset to SIG_DFL when the handler
  685.       function is called.  To get around this problem, on systems that
  686.       do not have wait3()/waitpid() but do have SIGCLD, you need to
  687.       reset the signal handler with a call to signal() after doing at
  688.       least one wait() within the handler, each time it is called.  For
  689.       backward compatibility reasons, System V will keep the old
  690.       semantics (reset handler on call) of signal().  Signal handlers
  691.       that stick can be installed with sigaction() or sigset().
  692.  
  693.       The summary of all this is that on systems that have waitpid()
  694.       (POSIX) or wait3(), you should use that and your signal handler
  695.       should loop, and on systems that don't, you should have one call
  696.       to wait() per invocation of the signal handler.
  697.  
  698.       One more thing -- if you don't want to go through all of this
  699.       trouble, there is a portable way to avoid this problem, although
  700.       it is somewhat less efficient.  Your parent process should fork,
  701.       and then wait right there and then for the child process to
  702.       terminate.  The child process then forks again, giving you a
  703.       child and a grandchild.  The child exits immediately (and hence
  704.       the parent waiting for it notices its death and continues to
  705.       work), and the grandchild does whatever the child was originally
  706.       supposed to.  Since its parent died, it is inherited by init,
  707.       which will do whatever waiting is needed.  This method is
  708.       inefficient because it requires an extra fork, but is pretty much
  709.       completely portable.
  710.  
  711. ------------------------------
  712.  
  713. Subject: How do I get lines from a pipe ... instead of only in larger blocks?
  714. >From: jik@rtfm.MIT.Edu (Jonathan I. Kamens)
  715. Date: Sun, 16 Feb 92 20:59:28 -0500
  716.  
  717. 3.14) How do I get lines from a pipe as they are written instead of only in
  718.       larger blocks?
  719.  
  720.       The stdio library does buffering differently depending on whether
  721.       it thinks it's running on a tty.  If it thinks it's on a tty, it
  722.       does buffering on a per-line basis; if not, it uses a larger
  723.       buffer than one line.
  724.  
  725.       If you have the source code to the client whose buffering you
  726.       want to disable, you can use setbuf() or setvbuf() to change the
  727.       buffering.
  728.  
  729.       If not, the best you can do is try to convince the program that
  730.       it's running on a tty by running it under a pty, e.g. by using
  731.       the "pty" program mentioned in question 3.9.
  732.  
  733. ------------------------------
  734.  
  735. End of unix/faq Digest part 3 of 7
  736. **********************************
  737.  
  738. -- 
  739. Ted Timar - tmatimar@empress.com
  740. Empress Software, 3100 Steeles Ave E, Markham, Ont., Canada L3R 8T3
  741.