home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / unix-faq / faq / part2 < prev    next >
Encoding:
Internet Message Format  |  2004-05-12  |  37.4 KB

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