Reputation: 167
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?
Upvotes: 1
Views: 680
Reputation: 23850
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
.
Upvotes: 1
Reputation: 118605
With all of @redneb's caveat's about sanitizing input
ssh remotehost FOO=bar \; export FOO \; script > out 2> err
or if you're sure that the shell on the remote host will be bash
, the more compact
ssh remotehost export FOO=bar \; script > out 2> err
Upvotes: 0