in reply to Re: Running Entire Bash Script Inside Perl
in thread Running Entire Bash Script Inside Perl

That design requires advance knowledge of the script (to change $1 to the parameter). I don't know if that's a problem.

The implementation fails if $param contains two spaces in a row, a quote, etc due to improper encoding of strings. Fix:

sub to_sh_lit { my ($s) = @_; die if !utf8::downgrade($s, 1); # Expecting bytes. die if $s =~ /\x00/; # NUL can't be passed. $s =~ s/('+)/'"$1"'/g; return "'$s'"; } my $lit_param = to_sh_lit($param); print SH "echo $lit_param\n";

Update: Added code.

Replies are listed 'Best First'.
Re^3: Running Entire Bash Script Inside Perl
by graff (Chancellor) on Jun 19, 2009 at 23:48 UTC
    That design requires advance knowledge of the script (to change $1 to the parameter).

    Well, there's no telling from the OP (as originally seen) how much is known in advance. Whatever the requirements are for the task, this approach could be adapted to use the available knowledge as needed, and work as well as other approaches.

    The implementation fails if $param contains two spaces in a row, a quote, etc.

    Sure. When printing commands to a shell process, there are lots of ways to go wrong and fail (and/or cause all sorts of mayhem and damage, depending on what sorts of mistakes are made and how various permissions play out when the perl script executes). This approach demands respect and caution; anyone who isn't sure whether they might get it wrong probably shouldn't use it.