这行代码如何在shell中运行?

时间:2017-01-24 04:32:13

标签: linux bash shell awk

我对这行代码感到困惑,有人能解释一下这段代码是如何工作的吗?我可以理解它使用管道,但中间的代码让我困惑。

for pid in $(ps -e -f | grep  $1 | grep -v $0 | awk '{print $2}')"

1 个答案:

答案 0 :(得分:0)

检查以下详细信息以了解命令,完整命令将如下 -

cat script.sh
for pid in $(ps -e -f | grep  $1 |grep -v $0 | awk '{print $2}')
do
kill -9 $pid
done

你需要像下面那样执行它 -

./script.sh vipin


step1 : ps -e -f (will print all the processes running in the server)
step2 : ps -e -f|grep $1 (considering $1 is variable for current user, 
        in my case is vipin(user) which i will pass with script,so step2
        will filter all the process for that user.

step3 : And $0 is the script name (script.sh), which you don't want to kill 
        that is why you are using grep -v(to exclude)
step4 : awk '{print $2}' to fetch only the process number.