[Linux-Discussion] Re: what does the variable $1 mean in Bourne shell?

  • From: ianezz@xxxxxxxxxx
  • To: linux-discussion@xxxxxxxxxxxxx
  • Date: Wed, 19 Dec 2001 10:34:56 +0100

David Bruce Jr, aggrappandosi alla tastiera per non cadere, ha scritto:

 > is that correct? sorta like a 'place holder'

Well, if you look at the bottom of it, every name of a variable is a
placeholder for its value, except for when one assigns values to
them... :-)

Basically, every time the shell finds ``$'' followed by a word
or by some special characters, it replaces (``expands'') that with the
value of a variable. There are some exceptions to this rule:

 - when $something is inside single quotes, i.e.

   something=1234
   echo '$something'

 - when the ``$'' is quoted with a backslash

   something=1234
   echo \$something


and then there are some special variables having special
values. Usually you don't assign to them, but their value changes
according to their context. I.e.

$1 ... $9 contains the parameters (arguments?) that one passed to your
script on the command line.

$? contains the numeric exit code of the last command executed by the shell
   (0 = ok or true, everything else means an error of some sort or false).
   See also later in this message.

$RANDOM gives a different random value each time you use it.

other variables have special meaning to the shell:

$PATH contains the list of all directories searched by the shell for
commands when you specify them on the command line or in a script
without giving a path (i.e. ``ls'' vs. ``/bin/ls'').

$PS1, $PS2 and $PS3 specify the prompts used by the shell in various
situations. The one you normally see is the one in $PS1. If you assign
values to them, the prompt changes.


 > man pages are almost gibberish

Man pages are reference pages, but reading the whole bash manpage
at least once usually is well worth the time :-)

 > lemme see, so if:
 > ! Expands to the process ID of the most recently executed background
 > (asynchronous) command.
 > 
 > then $! means a variable assigned to the most recently backgrounded command?

Exactly. The value of ``$!'' is the process id (a numeric value
uniquely identifying a process in the system) of the most recent
process started in background by that shell (i.e. specifying ``&'' as
the last char on the command line).

 > I'm trying to figure out our class project:
 > 
 > 
 > Write a shell script that does X, but first make sure the file exists
 > 
 > I'm working on the 'does the file exist or not' part.

Well, you have to understand some things first:

every process always returns a numeric exit code when it terminates. EVERY
process. Even if you kill it.

There is a convention that a process giving an exit code of ``0'' (zero)
means ``everything is OK'', or TRUE, and other values mean ``something
went wrong'' or FALSE.

Example:

if you execute

   ls /usr/bin

``ls'' lists the contents of /usr/bin, and then gives an exit code
of zero to say that everyting is ok.

On the other hand, if you execute

   ls /foobarbazasdfasdf

``ls'' complains that the ``/foobarbazasdfasdf'' directory doesn't
exist, and gives an exit code different from zero (i.e. 1, 2, or so).

You can see the exit code of the last excuted command with

   echo $?

because ``$?'' is a variable that contains just that. 
Try

   ls /usr/bin
   echo $?

and then

   ls /foobarbazasdfasdf
   echo $?

and compare the results.

----------------------------------------------------------------------

Then you have to understand how ``if ... then ... fi'' works. Its
syntax is:

   if command parameter parameter ...; then 
      ...
   else
      ...
   fi 

the shell see the ``if'' statement, and executes ``command'', and if
its exit value is 0 (which by convention means TRUE) it executes what
is immediately after ``then'', otherwise it executes what is
immediately after ``else'' (if there is an ``else'' at all).

So, for example, you could write:

    if ls -l /foobarbaz; then
        echo "Ok"
    else
        echo "Directory /foobarbaz doesn't exists"
    fi   

----------------------------------------------------------------------
Then you have to understand how ``['' and the ``test'' command are related

The  

    if [ expression ]; then 
       ... 
    fi

is the same as 

    if test expression; then 
       ... 
    fi 

Or, in other words, ``['' is just another name for the ``test''
command, and it looks nicer in shell scripts.

----------------------------------------------------------------------

The ``test'' command can perform several tests on values, files, etc.,
and of course you can use variables on its command line.

In particular, ``test'' can check for the existance of a file using
the ``-f'' option. So:

    if [ -f "$file" ]; then
       ...
    else
       echo "$file not found"
    fi

or, if you prefer:

    if test -f "$file"; then
       ...
    else
       echo "$file not found"
    fi

----------------------------------------------------------------------

 > if [ $? != 0 ]

this means: 

  ``if the exit code given by the last command is not equal to zero'' 

or, in other words
 
  ``if the last executed command gave an error of some sort''


the ``!='' operator is taken from the C language, and means "not
equal".

-- 
UNIX diapers by Pannolini USPTO 2039887  http://www.uspto.gov
Matteo Ianeselli      ianezz AT sodalia.it  (+39) 0461 316452
Visita il LinuxTrent:            http://www.linuxtrent.it

Other related posts: