Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Shell script with function

1 view
Skip to first unread message

Eran....@gmail.com

unread,
Feb 5, 2006, 3:58:15 AM2/5/06
to
Hi,

Is there a way for a function inside a shell script to receive
variable:
If so, what is the sending/receiving syntax?

TIA,
Eran.

kurt

unread,
Jun 14, 2006, 11:17:48 PM6/14/06
to
What is the source of that variable? If it's a parameter from the shell
or other application that calls the script, you can pass it on the
command tail, ie:

$ script <arg1> <arg2> <etc>

where args are string representations of your variables.

You can pass up to 8 (as I recall, may be more) command line arguments
to a bash script. If you need more, you can cat them to a file and read
them back in. You could pass the name of the file containing the
variable list to the script as a single argument. You can also set
environment variables and use them within your script.

$ VAR1="MyVar"
$ echo $VAR1
MyVar
$

You can also pass arguments on the command tail to a proper program. A
program written in "C", for instance, can receive up to 255 command line
arguments, and you can do with them what you will.

...kurt

Scott Lurndal

unread,
Jun 15, 2006, 1:15:16 PM6/15/06
to
kurt <ku...@olypen.com> writes:
>What is the source of that variable? If it's a parameter from the shell
>or other application that calls the script, you can pass it on the
>command tail, ie:
>
>$ script <arg1> <arg2> <etc>
>
>where args are string representations of your variables.
>
>You can pass up to 8 (as I recall, may be more) command line arguments

The only limit on the number of parameters to either a shell script or
a compiled program is the value of NCARGS for your system, which is
IIRC 8192 bytes in Linux. That means all of your arguments must fit
in a region of memory no larger than NCARGS. You could have 8192 1-byte
arguments or any combination that doesn't exceed NCARGS.

>to a bash script. If you need more, you can cat them to a file and read
>them back in. You could pass the name of the file containing the

Of course, pipes are the preferred way to "cat them to a file and read them back in".

>variable list to the script as a single argument. You can also set
>environment variables and use them within your script.

You can even provide "per-script" environment variables:

$ VAR1=fred VAR2=sam ./myscript arg1 arg2 ... argn


>
>$ VAR1="MyVar"
>$ echo $VAR1
>MyVar
>$
>
>You can also pass arguments on the command tail to a proper program. A
>program written in "C", for instance, can receive up to 255 command line
>arguments, and you can do with them what you will.

Many more than 255. See above.

in any case, your response is not responsive to the OP.

With respect to functions in shell scripts, they do accept
arguments:

function fred()
{
echo "arg1='${1}' args2='${2}'"
}

fred value1 value2


scott

kurt

unread,
Jun 15, 2006, 11:45:43 PM6/15/06
to
Thanks, Scott. After rereading the question I may have misinterpreted
what the OP was asking. Thank you for clearing up the number of passed
arguments question. In a recent project I was researching how to extract
arguments passed on the command line to a shell script and found several
recerences to "$1", "$2", etc. In several articles I read, it was
mentioned that only 9 (not 8 as I had originally remembered) arguments
could be passed. I tried it using the "$1" technique and my results
seemed to bear out this 9 argument limit (even though I typed "eight"
twice):

>$ ./getargs one two three four five six seven eight eight nine ten
eleven twelve
arg1 = one
arg2 = two
arg3 = three
arg4 = four
arg5 = five
arg6 = six
arg7 = seven
arg8 = eight
arg9 = eight
arg10 = one0
arg11 = one1
arg12 = one2

When running the script, after the 9th argument, "$10" is interpreted as
one0. I'm guessing there is a better way of extracting more than 9
arguments? Can you recommend a good tutorial (obviously I'm a relative
newbie to bash scripting)?

The number of arguments that can be passed being determined by a
pre-determined block of reserved space makes good sense. I'll be sure to
file that for future reference.

...kurt

0 new messages