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"; if($@){ 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 if
part and goes right on to installing it.
Advertisement
Answer
The if
works perfectly fine.
$ perl -e'system("if ! true ; then echo ok ; fi")' $ perl -e'system("if ! false ; then echo ok ; fi")' ok
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(EXPR)
with
system('/bin/bash', '-c', EXPR)
should do the trick.