Passing variables on the command line to a bash script

Passing variables on the command line to a bash script

Post by Timur Tab » Fri, 15 Jul 2005 01:37:59



I've searched the archives of this group, but I couldn't find an
answer, so either it's dumb question or I'm using the wrong search
keywords.

I'm trying to create a bash script that takes as command line
parameters a bunch of variables and their values.  Something like this:

Here's the 't' script:

#!/bin/sh
echo ${VAR}

And here's what I run

$ ./t VAR=4

I was hoping it would display "4", but it displays nothing.  What do I
need to do to get the script to set VAR to 4?  I guess I need to loop
through all the command-line parameters, and then "execute" each one,
but I don't know how to do that.  Obviously, this could be a security
hole, but I'm not concerned about that.  Any help is appreciated.

 
 
 

Passing variables on the command line to a bash script

Post by Ed Morto » Fri, 15 Jul 2005 01:46:48



> I've searched the archives of this group, but I couldn't find an
> answer, so either it's dumb question or I'm using the wrong search
> keywords.

> I'm trying to create a bash script that takes as command line
> parameters a bunch of variables and their values.  Something like this:

> Here's the 't' script:

> #!/bin/sh
> echo ${VAR}

> And here's what I run

> $ ./t VAR=4

> I was hoping it would display "4", but it displays nothing.  What do I
> need to do to get the script to set VAR to 4?  I guess I need to loop
> through all the command-line parameters, and then "execute" each one,
> but I don't know how to do that.  Obviously, this could be a security
> hole, but I'm not concerned about that.  Any help is appreciated.

It's very unclear what you're trying to do. You say you want to "get the
script to set VAR to 4" but then your example shows you attempting to
set VAR to 4 outside the script.

Are you trying to set a variable that can be used within "t" or trying
to get "t" to set a variable that can be used in the calling shell? If
it's the former, do this:

    VAR=4 ./t

or this:

    export VAR
    VAR=4
    ./t

If it's the latter, do this in "t"

    #!/bin/sh
    VAR="$1"

then call "t" as something like (depending on what you're really up to):

    . ./t 4
    echo "$VAR"

Note the "<dot><space>" at the start of the line.

If you're trying to do something else, do tell....

        Ed.

 
 
 

Passing variables on the command line to a bash script

Post by Lew Pitche » Fri, 15 Jul 2005 01:54:33


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> I've searched the archives of this group, but I couldn't find an
> answer, so either it's dumb question or I'm using the wrong search
> keywords.

> I'm trying to create a bash script that takes as command line
> parameters a bunch of variables and their values.  Something like this:

> Here's the 't' script:

> #!/bin/sh
> echo ${VAR}

> And here's what I run

> $ ./t VAR=4

[snip]

Put the VAR=4 /before/ the invocation of the script.

Like this...


  #!/bin/sh
  echo ${VAR}


  -rwxr--r--    1 pitchl   users          23 Jul 13 12:52 t


  1


  4


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC1UdJagVFX4UWr64RAlcgAJ0UpB1JW1V+md+KC0knbb76tZ63jQCfXMYF
cxcga8WLJJjq9PnZUHtZ37E=
=zVbk
-----END PGP SIGNATURE-----

 
 
 

Passing variables on the command line to a bash script

Post by Janis Papanagno » Fri, 15 Jul 2005 01:58:24



> I've searched the archives of this group, but I couldn't find an
> answer, so either it's dumb question or I'm using the wrong search
> keywords.

> I'm trying to create a bash script that takes as command line
> parameters a bunch of variables and their values.  Something like this:

> Here's the 't' script:

> #!/bin/sh
> echo ${VAR}

> And here's what I run

> $ ./t VAR=4

> I was hoping it would display "4", but it displays nothing.  What do I
> need to do to get the script to set VAR to 4?  I guess I need to loop
> through all the command-line parameters, and then "execute" each one,
> but I don't know how to do that.  Obviously, this could be a security
> hole, but I'm not concerned about that.  Any help is appreciated.

The program should know about its variable names. The call...

   ./t 4

The content of file t...

   VAR=$1
   echo ${VAR}

If you indeed want to handle an arbitrary and unknow variable name
use eval; for example...

   eval $1

(which is indeed a bad idea!)

Janis

 
 
 

Passing variables on the command line to a bash script

Post by Chris F.A. Johnso » Fri, 15 Jul 2005 03:54:07



Quote:> I've searched the archives of this group, but I couldn't find an
> answer, so either it's dumb question or I'm using the wrong search
> keywords.

> I'm trying to create a bash script that takes as command line
> parameters a bunch of variables and their values.  Something like this:

> Here's the 't' script:

> #!/bin/sh
> echo ${VAR}

> And here's what I run

> $ ./t VAR=4

> I was hoping it would display "4", but it displays nothing.  What do I
> need to do to get the script to set VAR to 4?  I guess I need to loop
> through all the command-line parameters, and then "execute" each one,
> but I don't know how to do that.  Obviously, this could be a security
> hole, but I'm not concerned about that.  Any help is appreciated.

    You are passing VAR=4 to your script as an argument; there is no
    assignment to the variable VAR.

    If you put the line "echo $1" in your script, it will print "VAR=4".

    You can assign the variable on the same line as the cal to your
    script, by putting it before the call:

VAR=4 ./t

    Or define it separately and export it:

VAR=4
export VAR
./t

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

1. Passing variables as arguments to commands in a shell script

Hi all,

I am a newbie with shell scripting and not sure if I am going about
this the right way.

What I have is:

  ADGroup="Domain Admins"
  cmd="ldapsearch -x -LLL"
  filter="CN=\"${ADgroup}\" member"
  ${cmd} ${filter}

I want the command to run as

  ldapsearch -x -LLL CN="Domain Admins" member

Instead what actually runs is

  ldapsearch -x -LLL 'CN="Domain' 'Admins"' member

How can I setup the variables so that I don't get the extra single
quotes when the command runs?

What also will work is

    ldapsearch -x -LLL CN='Domain Admins' member

But I have tried several combinations and couldn't get any to work with
the way my variables are setup.

Thanks,

Shahid

2. 1 * dlLMcxgIw-Mindbender of a firewall problem...

3. variable expansion on the bash command line

4. Server Response

5. bash and terminating background processes with variable substitutions on command line

6. printer form feed light stays on

7. bash: expansion on the command line: different quoting rules for variables and aliases?

8. AIX 4.2, Oracle Parallel Server, SMP Cluster Performance

9. Variable LINES in bash script

10. How to pass reg exp as command line argument in shell script?

11. too many filenames to pass on a command line to a perl script

12. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

13. remsh script - putting output of more than one command / variable on one line