[CS-FSLUG] Shell goodies

Brian Derr bderr at myrealbox.com
Fri Apr 2 22:56:30 CST 2004


Well, after putting it off for a while I thought I would give a few more
tricks/tips for the shell since there was a nice response to getting
more shell tricks.  If you already have a good grasp of the console you
may just want to skip over this because it is fairly easy stuff.  This
is mainly for someone new to the command line and doesn't know some of
the fundamentals of the UNIX world.  With that said, here we go:

1.)  Graves OR Backticks "`"
   Although I already gave an example of this in a previous email I'll
   go over them in more detail.  In `bash' the backtick is called "command
   substitution".  It is mainly used for setting of variables and
   shortening lengthy command line arguments.  Examples:

      ls -l `which bash`
      kill `pidof mutt`
      export VISUAL=`which vim`

   In essence what is happening is that the shell, `bash' in most cases,
   will start reading the script or command line and see the '`...`' and
   know it needs to execute what is contained therein.  So, in the first
   example `bash' will run the command "which bash" and place the output
   of the command in place of the call `which bash`.  On my system
   "which bash" returns /usr/bin/bash so what the shell will actually
   see and process is "ls -l /usr/bin/bash".  For your viewing pleasure
   here is a paste of this on my system:

      brian at brian:~$ ls -l `which bash`
      lrwxrwxrwx  1 root root 9 Jun 25  2003 /usr/bin/bash -> /bin/bash*
      
   Quite handy to place in the bag of tricks.

2.)  "Dot" command
   The "dot" command is simply a way of "sourcing" a file.  For
   instance, if I just edited my ~/.profile and changed my prompt (the
   PS1 variable) I can now type this:  . ~/.profile

   Rather than logout and login I can just "source" the file meaning
   it gets re-read.  The "dot" command can also be used in a `bash'
   script for adding a file to the script.  It is very similar to the
   "#include" directive of C/C++ when used in a script.

      #!/bin/bash
      
      . data_file
      ...
      
   More information about the "dot" command can be found in the Adv.
   Bash Scripting HOWTO
   (http://www.tldp.org/LDP/abs/html/internal.html#SOURCEREF).

3.)  I/O Redirection
   Have you ever ran a command that pumps gobs of information onto your
   console that you don't want to see?  Then this may be for you.  Lets
   pretend that `mpg321' doesn't have the "-q" option thus causing a lot
   of output to your terminal.  Call it with output redirection like so:
   
      mpg321 my_song.mp3 > /dev/null

   There is a way to separate error messages from normal output (STDERR
   {standard error} and STDOUT {standard output} respectively.)
   1 = STDOUT, 2 = STDERR.  Thus, if you run the `find' command with a
   starting path of '/' as a non-root user you are going to get A LOT of
   STDERR messages about not having permissions to read a directory.
   This is when redirection is really nice.

      find / -type f -iname 'my_regexp' 2> /dev/null

         OR

      find / -type f -iname 'my_regexp' 2> /dev/null 1> find.out

   The latter example will redirect all of the STDOUT to the file
   find.out thus leaving no messages on the console.  Nice if what you
   are searching for is going to be several screenfulls.  (Of course you
   could also "pipe", '|', the output to a pager, `less' for example.


   Of course, if you want you can also direct both STDERR and STDOUT to
   the same place.  You can use the '&>' redirection or you can link one
   to the other.  For instance if you want both STDOUT and STDERR to go
   to the same file you would do this:

      &> file_name

   Whereas if you wanted STDERR to go to the console with STDOUT
   (sometimes error messages get logged rather than tossed onto the
   console and you just may want to see those error messages) you can do
   the following:

      2>&1

   Both of the examples are tagged onto the end of your command,
   whatever it may be.  Unfortunately I can't think of any good examples
   at the moment.  :-\  What the second example does is attach STDERR to
   the STDOUT so that it goes where STDOUT goes, which is usually the
   console unless otherwise redirected.

   One other note about redirection is that you can append to a file.
   If you were to run a program and redirect the output to a file and
   want to run another program redirecting it into the same file if you
   were to use the '>' operator you would overwrite the file.  Hence
   '>>'!  This is the "append" operator and will append all the STDOUT
   (or STDERR if so desire) to the file.
   

Well, I hope I whetted your appetite for console goodness and didn't
mess something up in the meantime.  I use this stuff without thinking
about it usually so my explanations are probably poor.  If you are
thoroughly confused please let me know and I'll try to help clear things
up for you.  If any of you seasoned console veterans found some errors
please point them out (I'm getting sleepy as I finish this up.)  More to
come?

Brian

-- 
The just man walketh in his integrity:
his children are blessed after him.  -- Proverbs 20:7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://ofb.biz/pipermail/christiansource_ofb.biz/attachments/20040402/b5b098e8/attachment.sig>


More information about the Christiansource mailing list