[NTLUG:Discuss] bash script - function { } doesn't see command line options..

Richard Geoffrion ntlug at rain4us.net
Fri Jun 30 17:37:18 CDT 2006


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


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?

-- 
Richard



More information about the Discuss mailing list