home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / Documents / FAQ / Unix / Unix.fq2 < prev    next >
Text File  |  1993-01-27  |  29KB  |  831 lines

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