我如何最好地将参数传递给 Perl 单行程序? [英] How do I best pass arguments to a Perl one-liner?
本文介绍了我如何最好地将参数传递给 Perl 单行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,someFile
,像这样:
I have a file, someFile
, like this:
$cat someFile
hdisk1 active
hdisk2 active
我用这个shell脚本来检查:
I use this shell script to check:
$cat a.sh
#!/usr/bin/ksh
for d in 1 2
do
grep -q "hdisk$d" someFile && echo "$d : ok"
done
我正在尝试将其转换为 Perl:
I am trying to convert it to Perl:
$cat b.sh
#!/usr/bin/ksh
export d
for d in 1 2
do
cat someFile | perl -lane 'BEGIN{$d=$ENV{'d'};} print "$d: OK" if /hdisk$ds+/'
done
我在 shell 脚本中导出变量 d
并在 Perl 中使用 %ENV
获取值.有没有更好的方法将此值传递给 Perl 单行程序?
I export the variable d
in the shell script and get the value using %ENV
in Perl. Is there a better way of passing this value to the Perl one-liner?
推荐答案
您可以使用s"开关启用基本的命令行参数.为每个以破折号开头的参数定义了一个变量.--
告诉你的命令行参数从哪里开始.
You can enable rudimentary command line argument with the "s" switch. A variable gets defined for each argument starting with a dash. The --
tells where your command line arguments start.
for d in 1 2 ; do
cat someFile | perl -slane ' print "$someParameter: OK" if /hdisk$someParameters+/' -- -someParameter=$d;
done
参见:perlrun
这篇关于我如何最好地将参数传递给 Perl 单行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文