I have two scripts, namely shell_script.sh and perl_script.pl.
shell_script.sh : It has function definitions which, when invo
To use bash functions you need to be in bash. So in a Perl script that puts you inside backticks or system, where you are inside a bash process†. Then, inside that process, you can source the script with functions, what will bring them in, and execute them
funcs.sh
#!/bin/bash
function f1 {
t1=$1
u1=$2
echo "f1: t1=$t1 u1=$u1"
}
function f2 {
t2=$1
u2=$2
echo "f2: t2=$t2 u2=$u2"
}
and in Perl (one-liner)
perl -wE'
@r = qx(source funcs.sh; f1 a1 b1; f2 a2 b2);
print "got: $_" for @r
'
where qx is the operator for backticks, but perhaps clearer. I use backticks in case you need return from those functions. If your /bin/sh isn't linked to bash† then call bash explicitly
perl -wE'
@r = qx(/bin/bash -c "source funcs.sh; f1 a1 b1; f2 a2 b2");
print "got: $_" for @r
'
Assignment to an array puts qx in list context, in which it returns the STDOUT of what it ran as a list of lines. This can be used to separate return from different functions, if they return a single line each. The a1,b1 and a2,b2 are arguments passed to f1 and f2.
Prints
got: f1: t1=a1 u1=b1 got: f2: t2=a2 u2=b2
This makes some (reasonable) assumptions.
If there is no need for the return, but the functions only need to do their thing, you can use
system('/bin/bash', '-c', 'source ... ')
as in Håkon's answer
† It is /bin/sh really, but that is often relegated to bash. Check your system (/bin/sh is likely a link to another shell). Or ensure that bash runs the command
my @ret = qx( /bin/bash -c "source base.sh; f1 a1 b1; f2 a2 b2" );
See text for the explanation of this example.