gpt4 book ai didi

perl - 捕获 Perl 的 'system()' 的输出

转载 作者:行者123 更新时间:2023-12-03 10:19:39 24 4
gpt4 key购买 nike

我需要在 Perl 中使用 system() 运行 shell 命令。例如,

system('ls')
系统调用将打印到 STDOUT ,但我想将输出捕获到一个变量中,以便我可以用我的 Perl 代码进行 future 的处理。

最佳答案

这就是反引号的用途。来自 perldoc perlfaq8 :

Why can't I get the output of a command with system()?

You're confusing the purpose of system() and backticks (``). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.

my $exit_status   = system("mail-users");
my $output_string = `ls`;


perldoc perlop 更多细节。

关于perl - 捕获 Perl 的 'system()' 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com