0

I need to run a long piped command in ksh like this

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

Then execute this command. And then loop over the results. So I am trying the above line and then

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

And there can be more than two args as well in the result. I am not able to execute this and get the result what I want. Can somebody please suggest what is wrong in this and how i need to correct it.

4

1 回答 1

1

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

您可以改用一个函数:

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
于 2013-06-26T18:25:47.587 回答