开发者

How to execute Perl qx function with variable

How do I get Perl's 开发者_Go百科qx function to execute with my $opt variable?

Before (works):

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

I want to use either -k, -g, or -H:

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


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.

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: $!";


try to use backslash \$opt, it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜