It is currently Thu, 20 Jan 2022 03:31:43 GMT

Author Message
 Need Help calling variable within function
Hi guys,
I'm having trouble calling a variable within my function.
variable $1 for example is "1-5"
variable $2 for example is "6"
there are $# number of variables...

so I have a while loop to call my function for each variable..
while (( i <= $# )); do         #goes through loop till no more variables
        sed -n $(function $i) file1

the part I'm having trouble on is the exact value of the varible being
calling in the function...for example
when i=1, I want this to happen:
$(function $1)  where $1 is "1-5"
but what happens is
$(function 1)   cause i=1...rather than having the function being called
with the value of "1-5" going through it..
it must be a syntax error...does anyone know the correct syntax for this
command/calling of the function?

Sent via http://www.**-**.com/
Before you buy.

 Mon, 07 Apr 2003 03:00:00 GMT   
 Need Help calling variable within function

You have to use "eval" to get double evaluation of variables:

sed -n $(eval function '$'$i) file1

However, a better way would probably be to use an array.

set -A x "$@"
  sed -n $(function ${x[$i]} file1

Barry Margolin,
Genuity, Burlington, MA
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 Mon, 07 Apr 2003 03:00:00 GMT   
 Need Help calling variable within function

Try this:

    for var     # shorthand for "for var in $1 $2 ..."
        sed -n $(function $var) file1

With your syntax, you would have to use "eval" on the result of $i.


Bernd Eggink
Regionales Rechenzentrum der Uni Hamburg

 Mon, 07 Apr 2003 03:00:00 GMT   
 Need Help calling variable within function

what about  function "$i" file1 just a guess.

Tony Higgins
Verity, Inc.
892 Ross Dr, Sunnyvale, CA 94089, USA. Powering Business Portals
408 542 2131 (W)

 Mon, 07 Apr 2003 03:00:00 GMT   
   [ 4 post ] 

Similar Threads

1. Call function within a function

2. Need utility to trap network function calls (socket calls) and save to log file

3. Browser calls CGI C function which sets an env var and call a c function crashes

4. Need help with system() function call

5. function call with variable arguments

6. function call with variable nb. of parameters

7. Calling functions and passing variables?

8. Calling functions and passing variables?

9. how to know the instruction address of calling function within called function?

10. Variable substitution within a variable name

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software