【问题标题】:How does this line of code work in shell?这行代码在 shell 中是如何工作的?
【发布时间】:2017-01-24 04:32:12
【问题描述】:

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

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

【问题讨论】:

  • 只是awk部分你看不懂,还是别的?可能会帮助您查看每个阶段的输出:运行ps -e -f,然后运行ps -e -f | grep [whatever $1 is],依此类推。
  • 你应该改用pgreppkill
  • 细微的语法变化。将 awk 更改为 shell,因为它是一个通用的 shell 问题,而不是特别的 awk。添加标签。

标签: linux bash shell awk


【解决方案1】:

查看下面的详细信息以了解命令,完整的命令将是这样的-

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.

【讨论】:

  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-05-16
  • 2014-05-10
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多