gpt4 book ai didi

perl - 如何使用变量执行 Perl qx 函数

转载 作者:行者123 更新时间:2023-12-04 17:12:33 28 4
gpt4 key购买 nike

我如何获得 Perl 的 qx使用我的 $opt 执行的函数多变的?

之前(工作):

my @df_output = qx (df -k /tmp);

我想使用 -k , -g , 或 -H :
my @df_output = qx (df -$opt /tmp);

最佳答案

你所拥有的应该工作,但是 : 永远不要使用 qx .它古老而危险;无论你喂给它什么,它都会通过 shell,所以很容易受到 shell 注入(inject)的攻击,或者在 /bin/sh 的情况下遇到意外。不是你所期望的。

使用 open() 的多参数形式,它完全绕过了外壳。

open my $fh, '-|', 'df', "-$opt", '/tmp' or die "Can't open pipe: $!";
my @lines = <$fh>; # or read in a loop, which is more likely what you want
close $fh or die "Can't close pipe: $!";

关于perl - 如何使用变量执行 Perl qx 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378684/

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