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

  • From: "Damian Scott" <sdamian@xxxxxxxxxx>
  • To: linux-discussion@xxxxxxxxxxxxx
  • Date: Tue, 18 Dec 2001 17:16:10 -0800

One other thing you can use is $*.  That way you're not restricting your 
self to any limited number of arguments you can use. 



-Damian





"David Bruce Jr" <dbartbruce@xxxxxxxx>
Sent by: linux-discussion-bounce@xxxxxxxxxxxxx
12/18/01 04:02 PM
Please respond to linux-discussion

 
        To:     <linux-discussion@xxxxxxxxxxxxx>
        cc: 
        Subject:        [Linux-Discussion] Re: what does the variable $1 mean 
in Bourne shell?

 


Thanks,

now that you mention it I do remember that stuff about $1 is a variable 
for
what
the user puts in the first argument and $2 is a varable for what the user
put in as the
second argument...

is that correct? sorta like a 'place holder'

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

man pages are almost gibberish

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?

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.

an example from the text book is:

# Check for existance of file:
ls "$file" 2> /dev/null | grep "$file" > /dev/null
if [ $? != 0 ]
   then
         echo "$file not found" 1>&2
         exit 1


I get the ls the file and if its not there send the err msg to /dev/null
then pipe it to grep (and I get kinda lost there)

if test (using brackets instead of the test command) and I get
completely lost

what is $? !=0

I've no clue

thanks
David


----- Original Message -----
From: "Brian L. Johnson" <blj8@xxxxxxxx>
To: <linux-discussion@xxxxxxxxxxxxx>
Sent: Tuesday, December 18, 2001 6:38 PM
Subject: [Linux-Discussion] Re: what does the variable $1 mean in Bourne
shell?


>
> On Dec 18, David Bruce Jr did the finger mambo upon the keyboard:
>
> >$1 means "assign this value to a variable, so later on in the shell
script
> >all you
> >gotta do is use $1 and you don't have to write the whole file name"
> >I think
>
> Well, yes and no... $1, $2, etc are variables, but are special ones used
> to represent the commandline parameters. So if you run a script like
> this:
>
> scriptfile.sh bat box fruit ball car
>
> $1 would be "bat"
> $2 would be "box"
> etc...
>
> As for "$?":
>
> (From the man page for Bash)
>
>        *      Expands to the positional parameters, starting from
>               one.   When  the  expansion  occurs  within  double
>               quotes,  it expands to a single word with the value
>               of each parameter separated by the first  character
>               of  the  IFS  special variable.  That is, ``$*'' is
>               equivalent to ``$1c$2c...'', where c is  the  first
>               character of the value of the IFS variable.  If IFS
>               is null or unset, the parameters are  separated  by
>               spaces.
>        @      Expands to the positional parameters, starting from
>               one.   When  the  expansion  occurs  within  double
>               quotes,  each parameter expands as a separate word.
>               That is, `` $@'' is equivalent to ``$1'' ``$2'' ...
>               When there are no positional parameters, ``$@'' and
>               $@ expand to nothing (i.e., they are removed).
>        #      Expands to the number of positional  parameters  in
>               decimal.
>        ?      Expands to the status of the most recently executed
>               foreground pipeline.
>        -      Expands to the current option  flags  as  specified
>               upon  invocation,  by  the  set builtin command, or
>               those set by the  shell  itself  (such  as  the  -i
>               flag).
>        $      Expands  to  the  process ID of the shell.  In a ()
>               subshell, it expands to the process ID of the  cur=AD
>               rent shell, not the subshell.
>        !      Expands to the process ID of the most recently exe=AD
>               cuted background (asynchronous) command.
>        0      Expands to the name of the shell or  shell  script.
>               This  is  set  at shell initialization.  If bash is
>               invoked with a file of commands, $0 is set  to  the
>               name  of that file.  If bash is started with the -c
>               option, then $0 is set to the first argument  after
>               the string to be executed, if one is present.  Oth=AD
>               erwise, it is set to the pathname  used  to  invoke
>               bash, as given by argument zero.
>        _      Expands  to  the last argument to the previous com=AD
>               mand, after expansion.  Also set to the full  path=AD
>               name  of  each  command  executed and placed in the
>               environment exported to that command.
>
>   -=3DBrian L. Johnson,  www.blj8.com=3D-
>         9136 links at blj8.com!
>    ---------------------------------
>  For PGP key email to: pgpkey@xxxxxxxx
> For Geekcode email to: geekcode@xxxxxxxx
>
>
>
>







Other related posts: