如何最好地将参数传递给Perl单行代码? [英] How do I best pass arguments to a Perl one-liner?

查看:76
本文介绍了如何最好地将参数传递给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$d\s+/'
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$someParameter\s+/' -- -someParameter=$d; 
done

请参阅: perlrun

See: perlrun

这篇关于如何最好地将参数传递给Perl单行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆