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-part2 < prev    next >
Encoding:
Text File  |  1997-12-01  |  32.9 KB  |  978 lines

  1. Archive-name: unix-faq/faq/part2
  2. Version: $Id: part2,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.       2.1)  How do I remove a file whose name begins with a "-" ?
  42.       2.2)  How do I remove a file with funny characters in the filename ?
  43.       2.3)  How do I get a recursive directory listing?
  44.       2.4)  How do I get the current directory into my prompt?
  45.       2.5)  How do I read characters from the terminal in a shell script?
  46.       2.6)  How do I rename "*.foo" to "*.bar", or change file names
  47.               to lowercase?
  48.       2.7)  Why do I get [some strange error message] when I
  49.               "rsh host command" ?
  50.       2.8)  How do I {set an environment variable, change directory} inside a
  51.               program or shell script and have that change affect my
  52.               current shell?
  53.       2.9)  How do I redirect stdout and stderr separately in csh?
  54.       2.10) How do I tell inside .cshrc if I'm a login shell?
  55.       2.11) How do I construct a shell glob-pattern that matches all files
  56.             except "." and ".." ?
  57.       2.12) How do I find the last argument in a Bourne shell script?
  58.       2.13) What's wrong with having '.' in your $PATH ?
  59.       2.14) How do I ring the terminal bell during a shell script?
  60.       2.15) Why can't I use "talk" to talk with my friend on machine X?
  61.  
  62. If you're looking for the answer to, say, question 2.5, and want to skip
  63. everything else, you can search ahead for the regular expression "^2.5)".
  64.  
  65. While these are all legitimate questions, they seem to crop up in
  66. comp.unix.questions or comp.unix.shell on an annual basis, usually
  67. followed by plenty of replies (only some of which are correct) and then
  68. a period of griping about how the same questions keep coming up.  You
  69. may also like to read the monthly article "Answers to Frequently Asked
  70. Questions" in the newsgroup "news.announce.newusers", which will tell
  71. you what "UNIX" stands for.
  72.  
  73. With the variety of Unix systems in the world, it's hard to guarantee
  74. that these answers will work everywhere.  Read your local manual pages
  75. before trying anything suggested here.  If you have suggestions or
  76. corrections for any of these answers, please send them to to
  77. tmatimar@empress.com.
  78.  
  79. ----------------------------------------------------------------------
  80.  
  81. Subject: How do I remove a file whose name begins with a "-" ?
  82. Date: Thu Mar 18 17:16:55 EST 1993
  83.  
  84. 2.1)  How do I remove a file whose name begins with a "-" ?
  85.  
  86.       Figure out some way to name the file so that it doesn't begin
  87.       with a dash.  The simplest answer is to use
  88.  
  89.         rm ./-filename
  90.  
  91.       (assuming "-filename" is in the current directory, of course.)
  92.       This method of avoiding the interpretation of the "-" works with
  93.       other commands too.
  94.  
  95.       Many commands, particularly those that have been written to use
  96.       the "getopt(3)" argument parsing routine, accept a "--" argument
  97.       which means "this is the last option, anything after this is not
  98.       an option", so your version of rm might handle "rm -- -filename".
  99.       Some versions of rm that don't use getopt() treat a single "-"
  100.       in the same way, so you can also try "rm - -filename".
  101.  
  102. ------------------------------
  103.  
  104. Subject: How do I remove a file with funny characters in the filename ?
  105. Date: Thu Mar 18 17:16:55 EST 1993
  106.  
  107. 2.2)  How do I remove a file with funny characters in the filename ?
  108.  
  109.       If the 'funny character' is a '/', skip to the last part of this
  110.       answer.  If the funny character is something else, such as a ' '
  111.       or control character or character with the 8th bit set, keep reading.
  112.  
  113.       The classic answers are
  114.  
  115.     rm -i some*pattern*that*matches*only*the*file*you*want
  116.  
  117.     which asks you whether you want to remove each file matching
  118.     the indicated pattern;  depending on your shell, this may not
  119.     work if the filename has a character with the 8th bit set (the
  120.     shell may strip that off);
  121.  
  122.       and
  123.  
  124.     rm -ri .
  125.  
  126.     which asks you whether to remove each file in the directory.
  127.     Answer "y" to the problem file and "n" to everything else.
  128.     Unfortunately this doesn't work with many versions of rm.  Also
  129.     unfortunately, this will walk through every subdirectory of ".",
  130.     so you might want to "chmod a-x" those directories temporarily
  131.     to make them unsearchable.
  132.  
  133.     Always take a deep breath and think about what you're doing and
  134.     double check what you typed when you use rm's "-r" flag or a
  135.     wildcard on the command line;
  136.  
  137.       and
  138.  
  139.     find . -type f ... -ok rm '{}' \;
  140.  
  141.       where "..." is a group of predicates that uniquely identify the
  142.       file.  One possibility is to figure out the inode number of the
  143.       problem file (use "ls -i .") and then use
  144.  
  145.     find . -inum 12345 -ok rm '{}' \;
  146.  
  147.       or
  148.     find . -inum 12345 -ok mv '{}' new-file-name \;
  149.     
  150.       "-ok" is a safety check - it will prompt you for confirmation of
  151.       the command it's about to execute.  You can use "-exec" instead
  152.       to avoid the prompting, if you want to live dangerously, or if
  153.       you suspect that the filename may contain a funny character
  154.       sequence that will mess up your screen when printed.
  155.  
  156.       What if the filename has a '/' in it?
  157.  
  158.       These files really are special cases, and can only be created by
  159.       buggy kernel code (typically by implementations of NFS that don't
  160.       filter out illegal characters in file names from remote
  161.       machines.)  The first thing to do is to try to understand exactly
  162.       why this problem is so strange.
  163.  
  164.       Recall that Unix directories are simply pairs of filenames and
  165.       inode numbers.  A directory essentially contains information
  166.       like this:
  167.  
  168.     filename  inode
  169.  
  170.     file1      12345
  171.     file2.c      12349
  172.     file3     12347
  173.  
  174.       Theoretically, '/' and '\0' are the only two characters that
  175.       cannot appear in a filename - '/' because it's used to separate
  176.       directories and files, and '\0' because it terminates a filename.
  177.  
  178.       Unfortunately some implementations of NFS will blithely create
  179.       filenames with embedded slashes in response to requests from
  180.       remote machines.  For instance, this could happen when someone on
  181.       a Mac or other non-Unix machine decides to create a remote NFS
  182.       file on your Unix machine with the date in the filename.  Your
  183.       Unix directory then has this in it:
  184.  
  185.     filename  inode
  186.  
  187.     91/02/07  12357
  188.  
  189.       No amount of messing around with 'find' or 'rm' as described
  190.       above will delete this file, since those utilities and all other
  191.       Unix programs, are forced to interpret the '/' in the normal way.
  192.  
  193.       Any ordinary program will eventually try to do
  194.       unlink("91/02/07"), which as far as the kernel is concerned means
  195.       "unlink the file 07 in the subdirectory 02 of directory 91", but
  196.       that's not what we have - we have a *FILE* named "91/02/07" in
  197.       the current directory.  This is a subtle but crucial distinction.
  198.  
  199.       What can you do in this case?  The first thing to try is to
  200.       return to the Mac that created this crummy entry, and see if you
  201.       can convince it and your local NFS daemon to rename the file to
  202.       something without slashes.
  203.  
  204.       If that doesn't work or isn't possible, you'll need help from
  205.       your system manager, who will have to try the one of the
  206.       following.  Use "ls -i" to find the inode number of this bogus
  207.       file, then unmount the file system and use "clri" to clear the
  208.       inode, and "fsck" the file system with your fingers crossed.
  209.       This destroys the information in the file.  If you want to keep
  210.       it, you can try:
  211.  
  212.     create a new directory in the same parent directory as the one
  213.     containing the bad file name;
  214.  
  215.     move everything you can (i.e. everything but the file with the
  216.     bad name) from the old directory to the new one;
  217.  
  218.     do "ls -id" on the directory containing the file with the bad
  219.     name to get its inumber;
  220.  
  221.     umount the file system;
  222.  
  223.     "clri" the directory containing the file with the bad name;
  224.  
  225.     "fsck" the file system.
  226.  
  227.       Then, to find the file,
  228.  
  229.     remount the file system;
  230.  
  231.     rename the directory you created to have the name of the old
  232.     directory (since the old directory should have been blown away
  233.     by "fsck")
  234.  
  235.     move the file out of "lost+found" into the directory with a
  236.     better name.
  237.  
  238.       Alternatively, you can patch the directory the hard way by
  239.       crawling around in the raw file system.  Use "fsdb", if you
  240.       have it.
  241.  
  242. ------------------------------
  243.  
  244. Subject: How do I get a recursive directory listing?
  245. Date: Thu Mar 18 17:16:55 EST 1993
  246.  
  247. 2.3)  How do I get a recursive directory listing?
  248.  
  249.       One of the following may do what you want:
  250.  
  251.     ls -R             (not all versions of "ls" have -R)
  252.     find . -print        (should work everywhere)
  253.     du -a .            (shows you both the name and size)
  254.  
  255.       If you're looking for a wildcard pattern that will match all ".c"
  256.       files in this directory and below, you won't find one, but you
  257.       can use
  258.  
  259.     % some-command `find . -name '*.c' -print`
  260.  
  261.       "find" is a powerful program.  Learn about it.
  262.  
  263. ------------------------------
  264.  
  265. Subject: How do I get the current directory into my prompt?
  266. Date: Thu Mar 18 17:16:55 EST 1993
  267.  
  268. 2.4)  How do I get the current directory into my prompt?
  269.  
  270.       It depends which shell you are using.  It's easy with some
  271.       shells, hard or impossible with others.
  272.  
  273.       C Shell (csh):
  274.     Put this in your .cshrc - customize the prompt variable the
  275.     way you want.
  276.  
  277.         alias setprompt 'set prompt="${cwd}% "'
  278.         setprompt        # to set the initial prompt
  279.         alias cd 'chdir \!* && setprompt'
  280.     
  281.     If you use pushd and popd, you'll also need
  282.  
  283.         alias pushd 'pushd \!* && setprompt'
  284.         alias popd  'popd  \!* && setprompt'
  285.  
  286.     Some C shells don't keep a $cwd variable - you can use
  287.     `pwd` instead.
  288.  
  289.     If you just want the last component of the current directory
  290.     in your prompt ("mail% " instead of "/usr/spool/mail% ")
  291.     you can use
  292.  
  293.         alias setprompt 'set prompt="$cwd:t% "'
  294.     
  295.     Some older csh's get the meaning of && and || reversed.
  296.     Try doing:
  297.  
  298.         false && echo bug
  299.  
  300.     If it prints "bug", you need to switch && and || (and get
  301.     a better version of csh.)
  302.  
  303.       Bourne Shell (sh):
  304.  
  305.     If you have a newer version of the Bourne Shell (SVR2 or newer)
  306.     you can use a shell function to make your own command, "xcd" say:
  307.  
  308.         xcd() { cd $* ; PS1="`pwd` $ "; }
  309.  
  310.     If you have an older Bourne shell, it's complicated but not
  311.     impossible.  Here's one way.  Add this to your .profile file:
  312.  
  313.         LOGIN_SHELL=$$ export LOGIN_SHELL
  314.         CMDFILE=/tmp/cd.$$ export CMDFILE
  315.         # 16 is SIGURG, pick a signal that's not likely to be used
  316.         PROMPTSIG=16 export PROMPTSIG
  317.         trap '. $CMDFILE' $PROMPTSIG
  318.  
  319.     and then put this executable script (without the indentation!),
  320.     let's call it "xcd", somewhere in your PATH
  321.  
  322.         : xcd directory - change directory and set prompt
  323.         : by signalling the login shell to read a command file
  324.         cat >${CMDFILE?"not set"} <<EOF
  325.         cd $1
  326.         PS1="\`pwd\`$ "
  327.         EOF
  328.         kill -${PROMPTSIG?"not set"} ${LOGIN_SHELL?"not set"}
  329.  
  330.     Now change directories with "xcd /some/dir".
  331.  
  332.       Korn Shell (ksh):
  333.  
  334.     Put this in your .profile file:
  335.         PS1='$PWD $ '
  336.     
  337.     If you just want the last component of the directory, use
  338.         PS1='${PWD##*/} $ '
  339.  
  340.       T C shell (tcsh)
  341.  
  342.     Tcsh is a popular enhanced version of csh with some extra
  343.     builtin variables (and many other features):
  344.  
  345.         %~        the current directory, using ~ for $HOME
  346.         %/        the full pathname of the current directory
  347.         %c or %.    the trailing component of the current directory
  348.  
  349.     so you can do
  350.  
  351.         set prompt='%~ '
  352.  
  353.       BASH (FSF's "Bourne Again SHell")
  354.     
  355.     \w in $PS1 gives the full pathname of the current directory,
  356.     with ~ expansion for $HOME;  \W gives the basename of
  357.     the current directory.  So, in addition to the above sh and
  358.     ksh solutions, you could use
  359.  
  360.         PS1='\w $ '    
  361.     or
  362.         PS1='\W $ '
  363.  
  364. ------------------------------
  365.  
  366. Subject: How do I read characters from the terminal in a shell script?
  367. Date: Thu Mar 18 17:16:55 EST 1993
  368.  
  369. 2.5)  How do I read characters from the terminal in a shell script?
  370.  
  371.       In sh, use read.  It is most common to use a loop like
  372.  
  373.         while read line
  374.         do
  375.             ...
  376.         done
  377.  
  378.       In csh, use $< like this:
  379.     
  380.         while ( 1 )
  381.         set line = "$<"
  382.         if ( "$line" == "" ) break
  383.         ...
  384.         end
  385.  
  386.       Unfortunately csh has no way of distinguishing between a blank
  387.       line and an end-of-file.
  388.  
  389.       If you're using sh and want to read a *single* character from the
  390.       terminal, you can try something like
  391.  
  392.         echo -n "Enter a character: "
  393.         stty cbreak        # or  stty raw
  394.         readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  395.         stty -cbreak
  396.  
  397.         echo "Thank you for typing a $readchar ."
  398.  
  399. ------------------------------
  400.  
  401. Subject: How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  402. Date: Thu Mar 18 17:16:55 EST 1993
  403.  
  404. 2.6)  How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  405.     
  406.       Why doesn't "mv *.foo *.bar" work?  Think about how the shell
  407.       expands wildcards.   "*.foo" and "*.bar" are expanded before the
  408.       mv command ever sees the arguments.  Depending on your shell,
  409.       this can fail in a couple of ways.  CSH prints "No match."
  410.       because it can't match "*.bar".  SH executes "mv a.foo b.foo
  411.       c.foo *.bar", which will only succeed if you happen to have a
  412.       single directory named "*.bar", which is very unlikely and almost
  413.       certainly not what you had in mind.
  414.  
  415.       Depending on your shell, you can do it with a loop to "mv" each
  416.       file individually.  If your system has "basename", you can use:
  417.  
  418.       C Shell:
  419.     foreach f ( *.foo )
  420.         set base=`basename $f .foo`
  421.         mv $f $base.bar
  422.     end
  423.  
  424.       Bourne Shell:
  425.     for f in *.foo; do
  426.         base=`basename $f .foo`
  427.         mv $f $base.bar
  428.     done
  429.  
  430.       Some shells have their own variable substitution features, so
  431.       instead of using "basename", you can use simpler loops like:
  432.  
  433.       C Shell:
  434.  
  435.     foreach f ( *.foo )
  436.         mv $f $f:r.bar
  437.     end
  438.  
  439.       Korn Shell:
  440.  
  441.     for f in *.foo; do
  442.         mv $f ${f%foo}bar
  443.     done
  444.  
  445.       If you don't have "basename" or want to do something like
  446.       renaming foo.* to bar.*, you can use something like "sed" to
  447.       strip apart the original file name in other ways, but the general
  448.       looping idea is the same.  You can also convert file names into
  449.       "mv" commands with 'sed', and hand the commands off to "sh" for
  450.       execution.  Try
  451.  
  452.     ls -d *.foo | sed -e 's/.*/mv & &/' -e 's/foo$/bar/' | sh
  453.  
  454.       A program by Vladimir Lanin called "mmv" that does this job
  455.       nicely was posted to comp.sources.unix (Volume 21, issues 87 and
  456.       88) in April 1990.  It lets you use
  457.  
  458.     mmv '*.foo' '=1.bar'
  459.  
  460.       Shell loops like the above can also be used to translate file
  461.       names from upper to lower case or vice versa.  You could use
  462.       something like this to rename uppercase files to lowercase:
  463.  
  464.     C Shell:
  465.         foreach f ( * )
  466.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  467.         end
  468.     Bourne Shell:
  469.         for f in *; do
  470.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  471.         done
  472.     Korn Shell:
  473.         typeset -l l
  474.         for f in *; do
  475.         l="$f"
  476.         mv $f $l
  477.         done
  478.  
  479.       If you wanted to be really thorough and handle files with `funny'
  480.       names (embedded blanks or whatever) you'd need to use
  481.  
  482.     Bourne Shell:
  483.  
  484.         for f in *; do
  485.           g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
  486.           mv "$f" "$g"
  487.         done
  488.  
  489.       The `expr' command will always print the filename, even if it
  490.       equals `-n' or if it contains a System V escape sequence like `\c'.
  491.  
  492.       Some versions of "tr" require the [ and ], some don't.  It
  493.       happens to be harmless to include them in this particular
  494.       example; versions of tr that don't want the [] will conveniently
  495.       think they are supposed to translate '[' to '[' and ']' to ']'.
  496.  
  497.       If you have the "perl" language installed, you may find this
  498.       rename script by Larry Wall very useful.  It can be used to
  499.       accomplish a wide variety of filename changes.
  500.  
  501.     #!/usr/bin/perl
  502.     #
  503.     # rename script examples from lwall:
  504.     #       rename 's/\.orig$//' *.orig
  505.     #       rename 'y/A-Z/a-z/ unless /^Make/' *
  506.     #       rename '$_ .= ".bad"' *.f
  507.     #       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
  508.  
  509.     $op = shift;
  510.     for (@ARGV) {
  511.         $was = $_;
  512.         eval $op;
  513.         die $@ if $@;
  514.         rename($was,$_) unless $was eq $_;
  515.     }
  516.  
  517. ------------------------------
  518.  
  519. Subject: Why do I get [some strange error message] when I "rsh host command" ?
  520. Date: Thu Mar 18 17:16:55 EST 1993
  521.  
  522. 2.7)  Why do I get [some strange error message] when I "rsh host command" ?
  523.  
  524.       (We're talking about the remote shell program "rsh" or sometimes
  525.       "remsh" or "remote"; on some machines, there is a restricted shell
  526.       called "rsh", which is a different thing.)
  527.  
  528.       If your remote account uses the C shell, the remote host will
  529.       fire up a C shell to execute 'command' for you, and that shell
  530.       will read your remote .cshrc file.  Perhaps your .cshrc contains
  531.       a "stty", "biff" or some other command that isn't appropriate for
  532.       a non-interactive shell.  The unexpected output or error message
  533.       from these commands can screw up your rsh in odd ways.
  534.  
  535.       Here's an example.  Suppose you have
  536.  
  537.     stty erase ^H
  538.     biff y
  539.  
  540.       in your .cshrc file.  You'll get some odd messages like this.
  541.  
  542.     % rsh some-machine date
  543.     stty: : Can't assign requested address
  544.     Where are you?
  545.     Tue Oct  1 09:24:45 EST 1991
  546.  
  547.       You might also get similar errors when running certain "at" or
  548.       "cron" jobs that also read your .cshrc file.
  549.  
  550.       Fortunately, the fix is simple.  There are, quite possibly, a
  551.       whole *bunch* of operations in your ".cshrc" (e.g., "set
  552.       history=N") that are simply not worth doing except in interactive
  553.       shells.  What you do is surround them in your ".cshrc" with:
  554.  
  555.         if ( $?prompt ) then
  556.             operations....
  557.         endif
  558.  
  559.       and, since in a non-interactive shell "prompt" won't be set, the
  560.       operations in question will only be done in interactive shells.
  561.  
  562.       You may also wish to move some commands to your .login file; if
  563.       those commands only need to be done when a login session starts
  564.       up (checking for new mail, unread news and so on) it's better to
  565.       have them in the .login file.
  566.  
  567. ------------------------------
  568.  
  569. Subject: How do I ... and have that change affect my current shell?
  570. Date: Thu Mar 18 17:16:55 EST 1993
  571.  
  572. 2.8)  How do I {set an environment variable, change directory} inside
  573.       a program or shell script and have that change affect my
  574.       current shell?
  575.  
  576.       In general, you can't, at least not without making special
  577.       arrangements.  When a child process is created, it inherits a
  578.       copy of its parent's variables (and current directory).  The
  579.       child can change these values all it wants but the changes won't
  580.       affect the parent shell, since the child is changing a copy of
  581.       the original data.
  582.  
  583.       Some special arrangements are possible.  Your child process could
  584.       write out the changed variables, if the parent was prepared to
  585.       read the output and interpret it as commands to set its own
  586.       variables.
  587.  
  588.       Also, shells can arrange to run other shell scripts in the
  589.       context of the current shell, rather than in a child process, so
  590.       that changes will affect the original shell.
  591.  
  592.       For instance, if you have a C shell script named "myscript":
  593.  
  594.     cd /very/long/path
  595.     setenv PATH /something:/something-else
  596.  
  597.       or the equivalent Bourne or Korn shell script
  598.  
  599.     cd /very/long/path
  600.     PATH=/something:/something-else export PATH
  601.  
  602.       and try to run "myscript" from your shell, your shell will fork
  603.       and run the shell script in a subprocess.  The subprocess is also
  604.       running the shell; when it sees the "cd" command it changes *its*
  605.       current directory, and when it sees the "setenv" command it
  606.       changes *its* environment, but neither has any effect on the
  607.       current directory of the shell at which you're typing (your login
  608.       shell, let's say).
  609.  
  610.       In order to get your login shell to execute the script (without
  611.       forking) you have to use the "." command (for the Bourne or Korn
  612.       shells) or the "source" command (for the C shell).  I.e. you type
  613.  
  614.     . myscript
  615.  
  616.       to the Bourne or Korn shells, or
  617.  
  618.     source myscript
  619.  
  620.       to the C shell.
  621.  
  622.       If all you are trying to do is change directory or set an
  623.       environment variable, it will probably be simpler to use a C
  624.       shell alias or Bourne/Korn shell function.  See the "how do I get
  625.       the current directory into my prompt" section of this article for
  626.       some examples.
  627.  
  628.       A much more detailed answer prepared by
  629.       Thomas.Michanek@lin.infolog.se (Thomas Michanek) can be found at
  630.       ftp.wg.omron.co.jp in /pub/unix-faq/docs/script-vs-env.
  631.  
  632. ------------------------------
  633.  
  634. Subject: How do I redirect stdout and stderr separately in csh?
  635. >From: msb@sq.com (Mark Brader)
  636. Date: Mon, 26 Oct 1992 20:15:00 -0500
  637.  
  638. 2.9)  How do I redirect stdout and stderr separately in csh?
  639.  
  640.       In csh, you can redirect stdout with ">", or stdout and stderr
  641.       together with ">&" but there is no direct way to redirect stderr
  642.       only.  The best you can do is
  643.  
  644.     ( command >stdout_file ) >&stderr_file
  645.  
  646.       which runs "command" in a subshell;  stdout is redirected inside
  647.       the subshell to stdout_file, and both stdout and stderr from the
  648.       subshell are redirected to stderr_file, but by this point stdout
  649.       has already been redirected so only stderr actually winds up in
  650.       stderr_file.
  651.  
  652.       If what you want is to avoid redirecting stdout at all, let sh
  653.       do it for you.
  654.  
  655.     sh -c 'command 2>stderr_file'
  656.  
  657. ------------------------------
  658.  
  659. Subject: How do I tell inside .cshrc if I'm a login shell?
  660. Date: Thu Mar 18 17:16:55 EST 1993
  661.  
  662. 2.10) How do I tell inside .cshrc if I'm a login shell?
  663.  
  664.       When people ask this, they usually mean either
  665.  
  666.     How can I tell if it's an interactive shell?  or
  667.     How can I tell if it's a top-level shell?
  668.  
  669.       You could perhaps determine if your shell truly is a login shell
  670.       (i.e. is going to source ".login" after it is done with ".cshrc")
  671.       by fooling around with "ps" and "$$".  Login shells generally
  672.       have names that begin with a '-'.  If you're really interested in
  673.       the other two questions, here's one way you can organize your
  674.       .cshrc to find out.
  675.  
  676.     if (! $?CSHLEVEL) then
  677.         #
  678.         # This is a "top-level" shell,
  679.         # perhaps a login shell, perhaps a shell started up by
  680.         # 'rsh machine some-command'
  681.         # This is where we should set PATH and anything else we
  682.         # want to apply to every one of our shells.
  683.         #
  684.         setenv      CSHLEVEL        0
  685.         set home = ~username        # just to be sure
  686.         source ~/.env               # environment stuff we always want
  687.     else
  688.         #
  689.         # This shell is a child of one of our other shells so
  690.         # we don't need to set all the environment variables again.
  691.         #
  692.         set tmp = $CSHLEVEL
  693.         @ tmp++
  694.         setenv      CSHLEVEL        $tmp
  695.     endif
  696.  
  697.     # Exit from .cshrc if not interactive, e.g. under rsh
  698.     if (! $?prompt) exit
  699.  
  700.     # Here we could set the prompt or aliases that would be useful
  701.     # for interactive shells only.
  702.  
  703.     source ~/.aliases
  704.  
  705. ------------------------------
  706.  
  707. Subject: How do I construct a ... matches all files except "." and ".." ?
  708. Date: Thu Mar 18 17:16:55 EST 1993
  709.  
  710. 2.11) How do I construct a shell glob-pattern that matches all files
  711.       except "." and ".." ?
  712.  
  713.       You'd think this would be easy.
  714.  
  715.       *        Matches all files that don't begin with a ".";
  716.  
  717.       .*         Matches all files that do begin with a ".", but
  718.          this includes the special entries "." and "..",
  719.          which often you don't want;
  720.  
  721.       .[!.]*   (Newer shells only; some shells use a "^" instead of
  722.          the "!"; POSIX shells must accept the "!", but may
  723.          accept a "^" as well; all portable applications shall
  724.          not use an unquoted "^" immediately following the "[")
  725.  
  726.          Matches all files that begin with a "." and are
  727.          followed by a non-"."; unfortunately this will miss
  728.          "..foo";
  729.  
  730.       .??*     Matches files that begin with a "." and which are
  731.          at least 3 characters long.  This neatly avoids
  732.          "." and "..", but also misses ".a" .
  733.  
  734.       So to match all files except "." and ".." safely you have to use
  735.       3 patterns (if you don't have filenames like ".a" you can leave
  736.       out the first):
  737.  
  738.     .[!.]* .??* *
  739.  
  740.       Alternatively you could employ an external program or two and use
  741.       backquote substitution.  This is pretty good:
  742.  
  743.       `ls -a | sed -e '/^\.$/d' -e '/^\.\.$/d'`
  744.  
  745.     (or `ls -A` in some Unix versions)
  746.  
  747.       but even it will mess up on files with newlines, IFS characters
  748.       or wildcards in their names.
  749.  
  750. ------------------------------
  751.  
  752. Subject: How do I find the last argument in a Bourne shell script?
  753. Date: Thu Mar 18 17:16:55 EST 1993
  754.  
  755. 2.12) How do I find the last argument in a Bourne shell script?
  756.  
  757.       Answer by:
  758.     Martin Weitzel <@mikros.systemware.de:martin@mwtech.uucp>
  759.     Maarten Litmaath <maart@nat.vu.nl>
  760.  
  761.       If you are sure the number of arguments is at most 9, you can use:
  762.  
  763.     eval last=\${$#}
  764.  
  765.       In POSIX-compatible shells it works for ANY number of arguments.
  766.       The following works always too:
  767.  
  768.     for last
  769.     do
  770.         :
  771.     done
  772.  
  773.       This can be generalized as follows:
  774.  
  775.     for i
  776.     do
  777.         third_last=$second_last
  778.         second_last=$last
  779.         last=$i
  780.     done
  781.  
  782.       Now suppose you want to REMOVE the last argument from the list,
  783.       or REVERSE the argument list, or ACCESS the N-th argument
  784.       directly, whatever N may be.  Here is a basis of how to do it,
  785.       using only built-in shell constructs, without creating subprocesses:
  786.  
  787.     t0= u0= rest='1 2 3 4 5 6 7 8 9' argv=
  788.  
  789.     for h in '' $rest
  790.     do
  791.         for t in "$t0" $rest
  792.         do
  793.             for u in $u0 $rest
  794.             do
  795.                 case $# in
  796.                 0)
  797.                     break 3
  798.                 esac
  799.                 eval argv$h$t$u=\$1
  800.                 argv="$argv \"\$argv$h$t$u\""    # (1)
  801.                 shift
  802.             done
  803.             u0=0
  804.         done
  805.         t0=0
  806.     done
  807.  
  808.     # now restore the arguments
  809.     eval set x "$argv"                    # (2)
  810.     shift
  811.  
  812.       This example works for the first 999 arguments.  Enough?
  813.       Take a good look at the lines marked (1) and (2) and convince
  814.       yourself that the original arguments are restored indeed, no
  815.       matter what funny characters they contain!
  816.  
  817.       To find the N-th argument now you can use this:
  818.  
  819.     eval argN=\$argv$N
  820.  
  821.       To reverse the arguments the line marked (1) must be changed to:
  822.  
  823.     argv="\"\$argv$h$t$u\" $argv"
  824.  
  825.       How to remove the last argument is left as an exercise.
  826.  
  827.       If you allow subprocesses as well, possibly executing nonbuilt-in
  828.       commands, the `argvN' variables can be set up more easily:
  829.  
  830.     N=1
  831.  
  832.     for i
  833.     do
  834.         eval argv$N=\$i
  835.         N=`expr $N + 1`
  836.     done
  837.  
  838.       To reverse the arguments there is still a simpler method, that
  839.       even does not create subprocesses.  This approach can also be
  840.       taken if you want to delete e.g. the last argument, but in that
  841.       case you cannot refer directly to the N-th argument any more,
  842.       because the `argvN' variables are set up in reverse order:
  843.  
  844.     argv=
  845.  
  846.     for i
  847.     do
  848.         eval argv$#=\$i
  849.         argv="\"\$argv$#\" $argv"
  850.         shift
  851.     done
  852.  
  853.     eval set x "$argv"
  854.     shift
  855.  
  856. ------------------------------
  857.  
  858. Subject: What's wrong with having '.' in your $PATH ?
  859. Date: Thu Mar 18 17:16:55 EST 1993
  860.  
  861. 2.13) What's wrong with having '.' in your $PATH ?
  862.  
  863.       A bit of background: the PATH environment variable is a list of
  864.       directories separated by colons.  When you type a command name
  865.       without giving an explicit path (e.g. you type "ls", rather than
  866.       "/bin/ls") your shell searches each directory in the PATH list in
  867.       order, looking for an executable file by that name, and the shell
  868.       will run the first matching program it finds.
  869.  
  870.       One of the directories in the PATH list can be the current
  871.       directory "." .  It is also permissible to use an empty directory
  872.       name in the PATH list to indicate the current directory.  Both of
  873.       these are equivalent
  874.  
  875.       for csh users:
  876.  
  877.     setenv PATH :/usr/ucb:/bin:/usr/bin
  878.     setenv PATH .:/usr/ucb:/bin:/usr/bin
  879.  
  880.       for sh or ksh users
  881.  
  882.     PATH=:/usr/ucb:/bin:/usr/bin export PATH
  883.     PATH=.:/usr/ucb:/bin:/usr/bin export PATH
  884.  
  885.       Having "." somewhere in the PATH is convenient - you can type
  886.       "a.out" instead of "./a.out" to run programs in the current
  887.       directory.  But there's a catch.
  888.  
  889.       Consider what happens in the case  where "." is the first entry
  890.       in the PATH.  Suppose your current directory is a publically-
  891.       writable one, such as "/tmp".  If there just happens to be a
  892.       program named "/tmp/ls" left there by some other user, and you
  893.       type "ls" (intending, of course, to run the normal "/bin/ls"
  894.       program), your shell will instead run "./ls", the other user's
  895.       program.  Needless to say, the results of running an unknown
  896.       program like this might surprise you.
  897.  
  898.       It's slightly better to have "." at the end of the PATH:
  899.  
  900.     setenv PATH /usr/ucb:/bin:/usr/bin:.
  901.  
  902.       Now if you're in /tmp and you type "ls", the shell will
  903.       search /usr/ucb, /bin and /usr/bin for a program named
  904.       "ls" before it gets around to looking in ".", and there
  905.       is less risk of inadvertently running some other user's
  906.       "ls" program.  This isn't 100% secure though - if you're
  907.       a clumsy typist and some day type "sl -l" instead of "ls -l",
  908.       you run the risk of running "./sl", if there is one.
  909.       Some "clever" programmer could anticipate common typing
  910.       mistakes and leave programs by those names scattered
  911.       throughout public directories.  Beware.
  912.  
  913.       Many seasoned Unix users get by just fine without having
  914.       "." in the PATH at all:
  915.  
  916.     setenv PATH /usr/ucb:/bin:/usr/bin
  917.  
  918.       If you do this, you'll need to type "./program" instead
  919.       of "program" to run programs in the current directory, but
  920.       the increase in security is probably worth it.
  921.  
  922. ------------------------------
  923.  
  924. Subject: How do I ring the terminal bell during a shell script?
  925. >From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
  926. Date: Fri, 30 Apr 93 16:33:00 +0200
  927.  
  928. 2.9)  How do I ring the terminal bell during a shell script?
  929.  
  930.       The answer depends on your Unix version (or rather on the kind of
  931.       "echo" program that is available on your machine).
  932.  
  933.       A BSD-like "echo" uses the "-n" option for suppressing the final
  934.       newline and does not understand the octal \nnn notation.  Thus
  935.       the command is
  936.  
  937.         echo -n '^G'
  938.  
  939.       where ^G means a _literal_ BEL-character (you can produce this in
  940.       emacs using "Ctrl-Q Ctrl-G" and in vi using "Ctrl-V Ctrl-G").
  941.  
  942.       A SysV-like "echo" understands the \nnn notation and uses \c to
  943.       suppress the final newline, so the answer is:
  944.  
  945.         echo '\007\c'
  946.  
  947. ------------------------------
  948.  
  949. Subject: Why can't I use "talk" to talk with my friend on machine X?
  950. >From: tmatimar@empress.com (Ted Timar)
  951. Date: Thu Mar 18 17:16:55 EST 1993
  952.  
  953. 2.15) Why can't I use "talk" to talk with my friend on machine X?
  954.  
  955.       Unix has three common "talk" programs, none of which can talk with
  956.       any of the others.  The "old" talk accounts for the first two types.
  957.       This version (often called otalk) did not take "endian" order into
  958.       account when talking to other machines.  As a consequence, the Vax
  959.       version of otalk cannot talk with the Sun version of otalk.
  960.       These versions of talk use port 517.
  961.  
  962.       Around 1987, most vendors (except Sun, who took 6 years longer than
  963.       any of their competitors) standardized on a new talk (often called
  964.       ntalk) which knows about network byte order.  This talk works between
  965.       all machines that have it.  This version of talk uses port 518.
  966.  
  967.       There are now a few talk programs that speak both ntalk and one
  968.       version of otalk.  The most common of these is called "ytalk".
  969.  
  970. ------------------------------
  971.  
  972. End of unix/faq Digest part 2 of 7
  973. **********************************
  974.  
  975. -- 
  976. Ted Timar - tmatimar@empress.com
  977. Empress Software, 3100 Steeles Ave E, Markham, Ont., Canada L3R 8T3
  978.