home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / textfile / faqs / unix_faq / faq / part2 < prev    next >
Encoding:
Internet Message Format  |  1992-12-27  |  28.8 KB

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