Shell script with function
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Red Hat support > Red Hat Configuration > Shell script with function




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Shell script with function  
Eran.Yasso@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-05-06 12:47 PM

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.






[ Post a follow-up to this message ]



    Re: Shell script with function  
kurt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-15-06 06: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.
>





[ Post a follow-up to this message ]



    Re: Shell script with function  
Scott Lurndal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-15-06 06: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: 





[ Post a follow-up to this message ]



    Re: Shell script with function  
kurt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-16-06 06: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:07 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register