Ed Dunn Ed Dunn - 1 year ago 70
Bash Question

Executing an if else shell command from perl

I have never run into this scenario before so I could use a little help. I am trying to issue an if/else statement from a shell command but within perl. I've tried system and exec with no success. Here's a sample of the code

if ($os eq 'Ubuntu'){
system('if ! dpkg -l curl &> /dev/null; then apt-get install curl -y; fi');
foreach my $mod (keys %deb){
eval "use $mod";
system("apt-get install $deb{$mod} -y");
eval "use $mod";

If I run the command
if ! dpkg -l curl &> /dev/null; then apt-get install curl -y; fi
in shell it works fine but executing it from perl is skips the
part and goes right on to installing it.

Answer Source

The if works perfectly fine.

$ perl -e'system("if ! true ; then echo ok ; fi")'

$ perl -e'system("if ! false ; then echo ok ; fi")'

There is a problem, though. What you posted is a bash command, but system(EXPR) is short for system('/bin/sh', '-c', EXPR). Simply replacing



system('/bin/bash', '-c', EXPR)

should do the trick.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download