3

I have a user supplied script like

#!/bin/sh

some_function () {
    touch some_file
}

some_other_function () {
    touch some_other_file
}

and i want to call the function some_other_function from c-code.

I understand, that i could simply write a shell script like

#!/bin/sh

source userscript.sh
some_other_function

and execute it using system(), but i am looking for a more elegant and especially a more generic solution, that lets me execute arbitrarily named functions and maybe even lets me get/set variables.

7
  • c can't dynamically load interpreted code Commented Apr 14, 2014 at 19:43
  • c can exec an external program/shell, so exec('sh yourscript.sh foo bar baz');, basically, where foo bar baz become arguments to your external script. Commented Apr 14, 2014 at 19:45
  • You can execute function by sourcing bash and then calling the function. for example bash -c ". myfuncs.sh ; call_to_func arg1" Commented Apr 14, 2014 at 19:46
  • 1
    /bin/sh isn't bash (well, technically it might be provided by bash, but it's not the same thing). Commented Apr 14, 2014 at 19:46
  • @Steve Cox: I am aware of that. I already solved the problem in a way, as stated in the question. But i do not want to supply a seperate shellscript for every function i am calling from the shellscript. I am just looking for a more elegant/generic way to do it. Commented Apr 14, 2014 at 19:49

2 Answers 2

3

You cannot do this directly from C. However, you can use system to run a command (like sh) from C:

// Run the command: sh -c 'source userscript.sh; some_other_function'
system("sh -c 'source userscript.sh; some_other_function'");

(Note that the sh -c 'command' lets you run command in a shell.)

Alternatively, you can also use execlp or some other function from the exec family:

// Run the command: sh -c 'source userscript.sh; some_other_function'
execlp("sh", "sh", "-c", "source userscript.sh; some_other_function", NULL);

(Note here that when using exec functions, the first argument – "sh"must be repeated)

Sign up to request clarification or add additional context in comments.

Comments

1

From the comments, I understand that you want to call one of several functions defined in your script. You can do this, if you give the function as an argument to the shell script and in the last line just have $1, e.g.

fun1()
{
    echo "fun1 called"
}

fun2()
{
    echo "fun2 called"
}

$1

You can then call your script as

sh userscript.sh fun1

which gives

fun1 called

2 Comments

pretty sure this doesn't work, but could be wrong, but just tried with bash and you can not call / invoke a function in a bash script like this, at least i can't on my current box.
@ipatch It does work, just tried it myself. See example added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.