I've found it handy to be able to neatly encapsulate sh/bash shell scripts in here documents passed to system() as follows:
#! /usr/bin/perl sub whatever { system <<'SCRIPT'; date echo "what about this?" SCRIPT } whatever;
However, I've not been able to find a syntax that allows passing arguments to the shell script in the here document. I've been able to sort of work around it by passing environment variables like this:
#! /usr/bin/perl my $value = "testvalue"; sub whatever { system "MYVAR=$value;" . <<'SCRIPT'; date echo "arg1($MYVAR)" echo "what about this?" SCRIPT } whatever;
But that's unsatisfying. What I really want is to pass positional arguments to the scripts, and reference them via the likes of $* or $1 within the embedded shell script As Usual.
<LATER...>The following ALMOST works (nods to Murphy's Law), except for the fact that the first argument winds up in $0 instead $1..... ugh....
<LATER STILL...>#! /usr/bin/perl my $rc = system ("sh", "-c", <<'SCRIPT', "one", "two", "three"); echo 'this is a test' echo 'again' echo "0($0) 1($1) 2($2)" SCRIPT $rc >> 8; exit $rc;
Um, DUH! The above technique works fine if a pass a dummy program name argument first.....
#! /usr/bin/perl my $rc = system ("sh", "-c", <<'SCRIPT', "bogus_program_name", "one", +"two", "three"); echo 'this is a test' echo 'again' echo "0($0) 1($1) 2($2) 3($3)" SCRIPT $rc >> 8; exit $rc;
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |