linux - Executing a bash script from a Perl program -
i'm trying write perl program execute bash script. perl script looks this
#!/usr/bin/perl use diagnostics; use warnings; require 'userlib.pl'; use cgi qw(:standard); readparse(); $q = new cgi; $dir = $q->param('x'); $s = $q->param('y'); ui_print_header(undef, $text{'edit_title'}.$dir, ""); print $dir."<br>"; print $s."<br>"; print "under construction <br>"; use cwd; $pwd = cwd(); $directory = "/logs/".$dir."/logmanager/".$s; $command = $pwd."/script ".$directory."/".$s.".tar"; print $command."<br>"; print $pwd."<br>"; chdir($directory); $pwd1 = cwd(); print $pwd1."<br>"; system($command, $directory) or die "cannot open dir: $!"; the script fail following error:
can't exec "/usr/libexec/webmin/foobar/script /path/filename.tar": no such file or directory @ /usr/libexec/webmin/foobar/program.cgi line 23 (#3) (w exec) system(), exec(), or piped open call not execute named program indicated reason. typical reasons include: permissions wrong on file, file wasn't found in $env{path}, executable in question compiled architecture, or #! line in script points interpreter can't run similar reasons. (or maybe system doesn't support #! @ all.) i've checked permissions correct, tar file i'm passing bash script exists, , tried command line run same command i'm trying run perl script ( /usr/libexec/webmin/foobar/script /path/filename.tar ) , works properly.
in perl, calling system 1 argument (in scalar context) , calling several scalar arguments (in list context) different things.
in scalar context, calling
system($command) will start external shell , execute $command in it. if string in $command has arguments, passed call, too. example
$command="ls /"; system($commmand); will evaluate to
sh -c "ls /" where shell given entire string, i.e. command arguments. also, $command run normal environment variables set. can security issue, see here , here few examples why.
on other hand, if call system array (in list context), perl not call shell , give $command argument, rather try execute first element of array directly , give other arguments parameters. so
$command = "ls"; $directory = "/"; system($command, $directory); will call ls directly, without spawning shell in between.
back question: code says
my $command = $pwd."/script ".$directory."/".$s.".tar"; system($command, $directory) or die "cannot open dir: $!"; note $command here /path/to/script /path/to/foo.tar, argument being part of string. if call in scalar context
system($command) all work fine, because
sh -c "/path/to/script /path/to/foo.tar" will execute script foo.tar argument. if call in list context, try locate executable named /path/to/script /path/to/foo.tar, , fail.
Comments
Post a Comment