3

i got a shell script where the function name and its parameters is stored in variables. I don't know how to call it. I tried it with eval and without, but nothing works correctly.

example of my code:

VarFunction="Testfunc1"
VarName="Peter"
VarLastname="Lustig"
VarText="Is a really lucky guy!\n Maybe he knows some funny Stuff?"

eval ${VarFunction} "$VarName" "$VarLastname" "$VarText"


Testfunc1() {
     Name=$1
     LastName=$2
     Text=$3

     echo $Name 
     echo $Lastname
     echo $Text

}

When the function itself is not a variable, the script works fine.

Testfunc1 "$VarName" "$VarLastname" "$VarText"

But i want to call a function dynamicly.

How can i do that?

Thanks and greetings

Danloc

  • You do not need eval but if you use it then you should use it right: eval $VarFunction '"$VarName"' '"$VarLastname"' '"$VarText"' – Hauke Laging Jan 20 '18 at 16:30
  • 1
    "When the function itself is not a variable, the script works fine." That is very hard to believe. – Hauke Laging Jan 20 '18 at 16:32
  • 1
    Just out of curiosity, why do you store function name in a variable instead of calling function name directly ? This is kinda redundant. – Sergiy Kolodyazhnyy Jan 20 '18 at 18:17
4

This is about where you put the function definition. If you declare the function before it's called, you can call it even by variable. Try this:

VarFunction="Testfunc1"
VarName="Peter"
VarLastname="Lustig"
VarText="Is a really lucky guy!\n Maybe he knows some funny Stuff?"
Testfunc1() {
     Name=$1
     LastName=$2
     Text=$3

     echo $Name 
     echo $Lastname
     echo $Text 
}
${VarFunction} "$VarName" "$VarLastname" "$VarText"
  • That could be a problem for me. My programm does not have only one function and they are called from other functions in if clauses. So i can't be sure, that function1 is declared before function2 (who call function1). Is it possible to do like this? – Danloc Jan 20 '18 at 16:39
  • 1
    @Danloc You can write the whole lot in functions and then at the end of the script pack the call structure in the highest level function. And at the very end of the script call that function. This way all the functions are called after their definitions. And traditionally the main function is called... main. – Tomasz Jan 20 '18 at 16:48
  • Yea that is possible and maybe i have to do it like that. The reason why why i want do do it not like that, is because my programm is a Dialog menu programm. And so it runs in a permanent while true; loop. So it would be more beautiful, if every dialog can call any other dialog o every time For your solution i have to add more functions at the end of the programm, where the essential function calls go through. – Danloc Jan 20 '18 at 17:20
  • 1
    @Danloc btw, unix.stackexchange.com/questions/313256/… – Tomasz Jan 20 '18 at 17:28
  • 1
    As tomasz pointed out, there's something called main function. Define all functions first, including main() function. Particular order of declarations is irrelevant there. What's important is that you declare everything first, then call main at the end of script , i.e. make last line main "$@" . From there you can safely call other functions. – Sergiy Kolodyazhnyy Jan 20 '18 at 18:21
0

Define the function at the beginning. It works fine. What I understand is in shell, you need to define function first, before it is called.

#!/usr/bin/bash
Testfunc1() {
     Name=$1
     LastName=$2
     Text=$3

     echo $Name
     echo $Lastname
     echo $Text

}

VarFunction="Testfunc1"
VarName="Peter"
VarLastname="Lustig"
VarText="Is a really lucky guy!\n Maybe he knows some funny Stuff?"

${VarFunction} "$VarName" "$VarLastname" "$VarText"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.