如何使用变量执行Perl qx函数 [英] How to execute Perl qx function with variable
本文介绍了如何使用变量执行Perl qx函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使Perl的qx
函数与我的$opt
变量一起执行?
How do I get Perl's qx
function to execute with my $opt
variable?
(工作之前):
my @df_output = qx (df -k /tmp);
我想使用-k
,-g
或-H
:
my @df_output = qx (df -$opt /tmp);
推荐答案
您应该使用的工具,但是:永远不要使用qx
.这是古老而危险的;不管您输入的内容是经过外壳的,所以很容易受到外壳注入的影响,或者如果/bin/sh
并非您所期望的那样,您会感到惊讶.
What you have should work, but: don't ever use qx
. It's ancient and dangerous; whatever you feed to it goes through the shell, so it's very easy to be vulnerable to shell injection or run into surprises if /bin/sh
isn't quite what you expected.
使用open()
的多参数形式,完全绕过外壳.
Use the multi-arg form of open()
, which bypasses the shell entirely.
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 qx函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文