ksh脚本执行管道命令并循环结果

时间:2013-06-26 17:16:24

标签: ksh

我需要在ksh中像这样运行一个长的管道命令

cmd='ps -e -o args | /usr/bin/grep abcde | /usr/bin/grep -v grep'

然后执行此命令。 然后循环结果。 所以我正在尝试上面的行然后

$cmd | while read $arg1 $arg2 ; do
    echo $arg1 $arg2
    blah  $arg1 $arg2
done

结果中也可能有两个以上的参数。 我无法执行此操作并获得我想要的结果。 有人可以建议这有什么问题以及我需要如何纠正它。

1 个答案:

答案 0 :(得分:1)

为什么需要将pipline存储在变量中?

您可以改用函数:

find_process() {
    typeset search_pattern=$(sed 's/^./[&]/' <<<"$1")
    ps -e -o args | grep "$search_pattern"
}
find_process abcde 

请注意,pgrep可以做得更好。看看它是否已安装在您的系统上

关于“两个参数” - 你的问题是什么?您需要对ps结果做些什么?如果你有兴趣阅读一行文字,那么

... | while read -rA words; do
    : do something with the array "${words[@]}"
done
相关问题