Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a shell function from a Perl script?

Tags:

shell

perl

Say, I have a shell script like this:

utils.sh

function getDir
{
    echo "DirName"
}

I want to use that function from a Perl script:

test.pl

`source utils.sh`;

my $dir_name = `getDir`;

print $dir_name;

But this is not working. How I can get this done? Essentially I need to get the return value from a shell function to a Perl script.

like image 942
Golam Kawsar Avatar asked May 12 '26 07:05

Golam Kawsar


1 Answers

You'll need to call that function in the same shell that sources utils.sh, so:

my $dir_name = `source utils.sh; getDir`;
chomp($dir_name);
print $dir_name, "\n";
like image 134
Mat Avatar answered May 13 '26 22:05

Mat