Red Hat Configuration - Shell script with function

This is Interesting: Free IT Magazines  
Home > Archive > Red Hat Configuration > June 2006 > Shell script with function





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Shell script with function
Eran.Yasso@gmail.com

2006-02-05, 7:47 am

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

2006-06-15, 1:28 am

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


Eran.Yasso@gmail.com wrote:
> 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.
>

Scott Lurndal

2006-06-15, 1:24 pm

kurt <kurtl@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
[vbcol=seagreen]
>
>...kurt
>
>
>Eran.Yasso@gmail.com wrote:
kurt

2006-06-16, 1:28 am

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


Scott Lurndal wrote:
kurt <kurtl@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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com