acbh acbh - 22 days ago 4x
Perl Question

Passing an environment variable to a bash script over ssh from perl system

Currently I am executing run scripts using perl system by sshing into a remote machine:

system("ssh -t remote $dir/bashscript> $dir/stdout.stdout 2> $dir/stderr.stderr &");

I want to pass an environment variable to the bashscript on my remote machine (a directory to be specific). What is the best way to do it? And what should I add in my bashscript to accept the argument?


Try this:

system("ssh -t remote FOO='dir/dir/filename.stderr' $dir/bashscript> $dir/stdout.stdout 2> $dir/stderr.stderr &");

Note that if the value comes from an untrusted source this can be dangerous. You should really escape the value before you pass is like that. For example, you could do something like this:

my $foo='some value here';
$foo=~s/'/'\\''/g; # escape the '
system("ssh -t remote env FOO='$FOO' $dir/bashscript> $dir/stdout.stdout 2> $dir/stderr.stderr &");

In either case, bashscript can access the value via $FOO.