[NTLUG:Discuss] bash script - function { } doesn't see command line options..
Douglas Scott
dcscott1 at comcast.net
Fri Jun 30 20:18:51 CDT 2006
On Friday 30 June 2006 5:37 pm, Richard Geoffrion wrote:
> Take this test script, for example:
>
> #!/bin/bash
> function one () {
> echo I see one parameter and it is $1
> }
> function two () {
> echo I see two parameters, $1 and $2
> }
> if [ -z $2 ] ; then
> one
> else
> two
> fi
>
>
>
> Then call the script with either one or two parameters. I see that the
> logic is working but the functions themselves are not getting the
> scripts $1 variables passed through to the functions. My results were....
>
> $ ./func 1
> I see one paramater and it is
>
> $ ./func 2
> I see two parameters, and
>
[munch]
> For the moment I've taken to passing command line variables to the
> function calls when I call them....as like here.
>
>
> #!/bin/bash
> function one () {
> echo I see one parameter and it is $1
> }
> function two () {
> echo I see two parameters, $1 and $2
> }
> if [ -z $2 ] ; then
> one $@
> else
> two $@
> fi
>
>
> Is there a better way to accomplish what I'm trying to do?
If I remember correctly inside a function $1 means the first argument of the
function, not the script. So the last example is the only one that would
work.
In a number of ways script functions tend to act as if they are mini scripts.
Doug
More information about the Discuss
mailing list