home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / answers / unix-faq / faq / part2 < prev    next >
Encoding:
Internet Message Format  |  1993-12-26  |  32.0 KB

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