【发布时间】:2020-10-15 20:18:25
【问题描述】:
当我运行命令时
ps -ef | grep -i this | head -n 1 | awk '{print $2}' 在我的终端中,它为我提供了正确的输出,从而为我提供了“此”服务所需的 PID。
但是当我尝试将它放在 shell 脚本变量中时
PID=ps -ef | grep stress | head -n 1 | awk '{print $2}'
并尝试运行脚本,输出为line 9: -ef: command not found
救命!
【问题讨论】:
-
不需要 grep 和 head。更简单的是
PID=$(ps -ef |awk '/stress/ {print $2; exit}')