home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / unix-faq / faq.part3 < prev    next >
Encoding:
Text File  |  1995-07-25  |  31.6 KB  |  749 lines

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