【发布时间】:2021-01-24 09:36:29
【问题描述】:
当我使用open 函数时,我想使用bash 而不是sh。例如:
sub run_cmd {
my ($cmd) = @_;
my $fcmd;
print("Run $cmd\n");
open($fcmd, "$cmd |");
while ( my $line = <$fcmd> ) {
print "-> $line";
}
close $fcmd;
}
eval{run_cmd('ps -p $$')};
这里的输出是:
Run ps -p $$
-> PID TTY TIME CMD
-> 189493 pts/6 00:00:00 sh
我们可以看到默认使用sh。
我有一些限制,我需要 (1) 使用 open 函数和 (2) 调用 bash 而不是 sh。我尝试在命令开头添加bash,但它不起作用:
Run ps -p $$
/bin/ps: /bin/ps: cannot execute binary file
如何将bash 与open 函数一起使用?
【问题讨论】:
-
一个可能对您有帮助的相关问题:Which shell does a Perl system() call use?
-
@Pierre :除了tripleee 给出的正确答案之外,您看到的错误可能来自
PATH环境变量的错误设置。我建议,为了测试,你在运行命令之前打印它的值。 -
@user1934428 不,将
bash直接放在前面只会导致bash尝试将ps作为脚本文件执行。 -
@tripleee - 对,我应该正确阅读错误消息。或者当然
-c不见了。