执行shell命令不会产生任何结果

PMa*_*Mat 1 perl busybox docker alpine-linux

我在我的Perl脚本中运行shell命令,但它没有按预期工作.我是一个以Alpine Linux为基础的容器.

我的Perl版本v5.24.0.

perl -e 'my $TEST = `ls -al`; print $TEST'
Run Code Online (Sandbox Code Playgroud)

这没什么打印,但适用于我在Red Hat Linux上使用Perl v5.6.1的另一个系统.

Rob*_*ert 5

可能是你的问题.

我认为你没有定义PATH env var.

使用ls(/ bin/ls)的完整路径尝试此操作:

perl -e 'my $TEST = `/bin/ls -al`; print $TEST'
Run Code Online (Sandbox Code Playgroud)

我测试了类似的场景:

清空路径:

PATH="" /usr/bin/perl -e'
   my $output = `ls -al`;
   if (!defined($output)) {
      die("readpipe: $!\n")                             if $? == -1;
      die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
      die("Child exited with error ".( $? >> 8 )."\n")  if $? >> 8;
   }

   print($output);
'
Run Code Online (Sandbox Code Playgroud)

输出:

readpipe: No such file or directory
Run Code Online (Sandbox Code Playgroud)

使用绝对路径(/ bin/ls):

PATH="" /usr/bin/perl -e'
       my $output = `/bin/ls -al`;
       if (!defined($output)) {
          die("readpipe: $!\n")                             if $? == -1;
          die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
          die("Child exited with error ".( $? >> 8 )."\n")  if $? >> 8;
       }

   print($output);
'
Run Code Online (Sandbox Code Playgroud)

输出:

total 4531444
drwxr-xr-x  7 rbravo rbravo       4096 May 30 19:40 .
drwxr-xr-x 63 root   root         4096 Apr  7 14:11 ..
-rw-------  1 rbravo rbravo      12248 May 30 19:39 .bash_history
...
Run Code Online (Sandbox Code Playgroud)