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