【发布时间】:2010-07-23 19:46:35
【问题描述】:
我有一个文件,someFile,如下所示:
$cat someFile
hdisk1 active
hdisk2 active
我用这个shell脚本来检查:
$cat a.sh
#!/usr/bin/ksh
for d in 1 2
do
grep -q "hdisk$d" someFile && echo "$d : ok"
done
我正在尝试将其转换为 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 单行器?
【问题讨论】:
-
为什么不直接在 Perl 中执行循环呢?然后你的 shell 脚本就消失了。
-
使用
cat file | perl ...比使用perl ... file更简单。
标签: perl