Is there a way for a function inside a shell script to receive
variable:
If so, what is the sending/receiving syntax?
TIA,
Eran.
$ 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
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
>$ ./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