georgetovrea
georgetovrea

Reputation: 577

How to call shell script function in my Perl?

I have some functions in util.sh, I want to call function in util.sh and get the return value. Does anyone know how to do?

util.sh

#!/bin/bash
sayhello()
{
    echo $1 "hello"
}

I try to call the function in terminal using the command as below, but got the output "can not found the command sayhello":

sh /home/adnrew/code/test/util.sh sayhello test

and using the code in my perl as below,

$returnvalue = system("/home/adnrew/code/test/util.sh sayhello test ");

can not work too.

How can I call shell script function in my Perl?

Upvotes: 1

Views: 395

Answers (1)

Zzyzx
Zzyzx

Reputation: 531

You could source the shell script into the subprocess by

$returnvalue = system(". /home/adnrew/code/test/util.sh; sayhello test");

The parent process (a bash shell, or a perl system subprocess) creates a child bash process when it tries executing a bash shell. Anything defined in the script are thus limited only to the child process and will not reflect in the parent process itself. Sourcing a script (the 'dot' command or just source), on the other hand, runs the definition in the same bash process.

Upvotes: 2

Related Questions