Calling a function through a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calling a function through a variable
# 1  
Old 09-29-2009
Calling a function through a variable

Hey folks,

I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected.

Code:
 
#!/bin/ksh
 
. ./functions.sh
 
STRING=function_1
FUNCTION="$STRING"
RETURN=eval $FUNCTION
echo "value of $FUNCTION function is: $RETURN"

All i'm trying to do is evalute function_1 and use the function's name and return value in an echo statement.

With the above, the 'RETURN=eval $FUNCTION' echos the evaluated function to the screen. While $RETURN is returning nothing.

Any idea what i'm doing wrong?

The reason i'm doing things this way is that I want to eventually stick this is a loop and have this run for all functions that i chose to call (in a seperate file)... but baby steps and all that.

TIA

Daithí
# 2  
Old 09-29-2009
You are getting the null out put and you must me from this code, because you are just trying to get the value stored in "$FUNCTION" which is nothing the undefined variable(function_1).

By the way , are you getting any error like "function_1 command not found"


Regards,
Sanjay
# 3  
Old 09-29-2009
Maybe this is what you are looking for:

Code:
$> cat lib
somefunk()
{
 echo somestring
 return 7
}
$> cat mach.sh
. ./lib

echo `eval somefunk` $?
$> ./mach.sh
somestring 7

# 4  
Old 09-29-2009
I'm not getting the "function_1 command not found" error anymore. I was getting something similar to that when trying to run the functions.sh script using './functions.sh' instead of '. ./functions.sh' but have no idea why that was.

let's say i have function_1 is instead the following:
Code:
now()
{
    date +%Y%m%d%H%M%S
}

so when i run test.sh
Code:
. ./functions.sh
###################################################
RETURN=""
FUNCTION=""
STRING=""
####################################################
echo
STRING=now
FUNCTION="$STRING"
RETURN=eval $FUNCTION
echo "value of $FUNCTION function is: $RETURN"
echo

i get the following
Code:
20090929100109
value of now function is:

what i want to see is
Code:
value of now function is: 20090929100109

I suppose what i'm asking is can i evaluate a function and assign that return value to a variable?
# 5  
Old 09-29-2009
Am I missing something?

Code:
bash -c '
  now() { 
    date +%Y%m%d%H%M%S
    }
  STRING=now FUNCTION=$STRING
  RETURN="$($FUNCTION)"
  printf "return is: %s\n" "$RETURN"
  '

outputs:
Code:
zsh-4.3.10[sysadmin]% bash -c '
  now() {
    date +%Y%m%d%H%M%S
}
  STRING=now FUNCTION=$STRING
  RETURN="$($FUNCTION)"
  printf "return is: %s\n" "$RETURN"
  '
return is: 20090929111720

This should work with ksh, bash and zsh.
# 6  
Old 09-29-2009
Thanks radoulov. That worked perfectly.

Code:
RETURN="$($FUNCTION)"

this was all i needed. my unix is very rusty. better dust off the training manuals again.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

4. UNIX for Dummies Questions & Answers

Getting a error while calling a function

Hi Friends, While calling a function in below scipt func_serv_logs () { find . -type f \( \( -name 'WLS*' -o -name 'access*' \) -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print | while read FILENAME do echo "hi" done } func_serv_logs I am getting error... (4 Replies)
Discussion started by: Jcpratap
4 Replies

5. Shell Programming and Scripting

Scripting: Calling Another Function

Hi Guys, How to make a code/script which behaves like some kind of "front-end" then upon choosing from the options within it, it will call another script for that option. To make it clearer, let's say: a. I have a first script which will list all the names of Students. b. Then, once this... (1 Reply)
Discussion started by: rymnd_12345
1 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

9. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies
Login or Register to Ask a Question