[NTLUG:Discuss] bash script - function { } doesn't see command line options..
Chris Cox
cjcox at acm.org
Fri Jun 30 21:59:59 CDT 2006
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....
You didn't pass any arguments to the functions.
Try:
if [ -z $2 ]; then
one "$1"
else
two "$1" "$2"
fi
>
> $ ./func 1
> I see one paramater and it is
>
> $ ./func 2
> I see two parameters, and
>
>
> I've tried
>
> function one {
> function one() {
> function one () {
> one {
> one() {
> one () {
>
>
> NONE of those attempts were the correct syntax nor could I find any
> functional difference with the syntax in the above examples. I can't
> find any working examples to pass the command line variables to the
> functions.
>
> 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?
>
More information about the Discuss
mailing list