Hadar Epstein
Hadar Epstein

Reputation: 5

Usage of Perl custom script from shell

I'm trying to pass a variable to a custom Perl script.

        $file="my_wa/fin"
        if [[ "$file" == *"fin" ]]; then
            script.pl 
        fi

In my script I try to use $file as my path to open and read a file

my $input_filename = "$ENV{file}";

I understand that I need to export it somehow and receive it in my Perl code, but it doesn't recognize $file. - it works perfectly if I just write my_wa/fin.

Upvotes: 0

Views: 66

Answers (3)

hoffmeister
hoffmeister

Reputation: 612

you can pass $file as an argument

$file="my_wa/fin"
    if [[ "$file" == *"fin" ]]; then
        script.pl \"$file\"
    fi

Then access the data

my $input_filename = $ARGV[0];

Upvotes: 0

Lye Heng Foo
Lye Heng Foo

Reputation: 1879

Yes, you will need to export the environment variable file in your shell before the perl script execution. Also you may want to make sure to include full path to the file.

export file=/full/path/to/my/file/my_wa/fin

Or making use of pwd if it is relative to current working directory:

export file="$(pwd)/my_wa/fin"

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247210

You did not export the variable in the shell before calling the perl program.

Paste your shell code into http://www.shellcheck.net/ -- it will tell you your other shell error.

Upvotes: 1

Related Questions