Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run the output of a script as a standalone bash command

suppose you have a perl script "foobar.pl" that prints the following to stdout

date -R

and you want to run whatever that perl script outputs as a standalone bash command (don't worry about security problems as this is running in a trusted environment).

How do you get bash to recognize this as a standalone command?

I've tried using xargs, but that seems to want to pass arguments only to a pre-defined command.

I want the perl script to be able to output any arbitrary command.

$command = 'date -R'
system($command); ## in the perl script 

the above does not work because I want it to run in an existing cygwin environment ...

foobar.pl | xargs bash -i {}

the above does not work because bash seems to be running a new process and thus the initialization and settings from bash_profile don't get instantiated.

like image 656
dreftymac Avatar asked Apr 06 '09 02:04

dreftymac


2 Answers

`foobar.pl`
like image 114
Brian Campbell Avatar answered Sep 20 '22 01:09

Brian Campbell


Bad:

`perl foo.pl`
$(perl foo.pl)

Why is this bad? Because of so many reasons; most notably:

  • Wordsplitting: What you're doing here is taking the output of the perl script, splitting it into chunks wherever there are spaces, tabs or newlines, and taking those chunks as arguments to the first chunk which is the command to run. In really extremely simplistic cases like $(echo 'date +%s') it might work; but that's just a really bad representation of what you're REALLY doing here.
  • You cannot do quoting or use any other bash shell features like parameter expansion, bash keywords, etc.

Good, but inconvenient:

perl foo.pl > mytmpfile; bash mytmpfile

Creating a temporary file to put your perl script's output into and then running that with bash works, but it's inconvenient as you need to create (and clean up!) your temporary file and have it in a portably writable (and secure!) location.

Also remember not to use . or source to execute the temporary file unless you really intend to run it all in the active shell. Moreover, when you use . or source, you won't be able to reliably clean up your temporary file afterward.

Probably the best solution:

perl foo.pl | bash

This is pretty safe all-round ("safe" in the context of, least bug-prone) assuming your perl script outputs correct bash syntax, of course.

Alternatives that do pretty much the same thing:

bash < <(perl foo.pl)
bash <(perl foo.pl)
like image 44
lhunath Avatar answered Sep 19 '22 01:09

lhunath