Golam Kawsar
Golam Kawsar

Reputation: 780

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

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.

Upvotes: 2

Views: 2848

Answers (1)

Mat
Mat

Reputation: 206859

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";

Upvotes: 9

Related Questions