Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

passing arguments to a shell script in a here document passed to system() ?

by jethrick (Initiate)
on Jun 09, 2011 at 15:06 UTC ( #908931=perlquestion: print w/ replies, xml ) Need Help??
jethrick has asked for the wisdom of the Perl Monks concerning the following question:

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....

#! /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;
<LATER STILL...>

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;

Comment on passing arguments to a shell script in a here document passed to system() ?
Select or Download Code
Re: passing arguments to a shell script in a here document passed to system() ?
by choroba (Abbot) on Jun 09, 2011 at 15:19 UTC
    I would use
    system << "SCRIPT"; date echo "arg1($value)" SCRIPT
    i.e. double quote STRING and use perl variables (quoted if needed) without introducing environment ones. You have to escape script variables, though: \$var.

    If you need $@ and shifting, write the script to a file and run it :)

      Thanks. I thought of that, but I'd like to avoid having to escape script variables, so I can easily slap existing, working scripts into perl using <<'SCRIPT' to avoid introducing bugs to what already works.

      Which I suppose begs the question as to why I wouldn't simply use the original scripts as is. Well, I sometimes like consolidating scripts into a single "swiss army knife-ish" script to simplify dragging the functionality around.

Re: passing arguments to a shell script in a here document passed to system() ?
by i5513 (Monk) on Jun 10, 2011 at 07:30 UTC
    I propose using set builtin function of bash before pasting your code. You can adjust this code to receive a list of args, I guess:
    #! /usr/bin/perl sub whatever { my ($arg1,$arg2)=@_; system << "ARGS" . <<'SCRIPT'; if [ -n "$arg1" ] then set $arg1 $arg2 fi ARGS date echo "what about this?" echo $1 $2 SCRIPT } whatever $ARGV[0], $ARGV[1];
    Or more generic :
    #! /usr/bin/perl sub whatever { system << "ARGS" . <<'SCRIPT'; if [ -n "@_" ] then set @_ fi ARGS date echo "what about this?" echo $* SCRIPT } whatever @ARGV;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://908931]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-12-27 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls