gpt4 book ai didi

bash - 是否可以将 if 语句放入管道中?

转载 作者:行者123 更新时间:2023-12-04 18:55:53 25 4
gpt4 key购买 nike

ps aux | grep node | grep -v grep | awk '{print $2}' | xargs kill -9

我使用上述命令杀死所有 Node.js 进程(Ubuntu),但如果没有节点进程正在运行,它将显示错误(stderr)。是否可以在管道中使用 if 语句来避免 xargs什么都得不到?

就像是:
ps aux | grep node | grep -v grep | awk '{print $2}' | if [ $pip ] ; then
xargs kill -9 $pip
fi

最佳答案

man xargs :

   -r, --no-run-if-empty
If the standard input does not contain any nonblanks,
do not run the command. Normally, the command is run
once even if there is no input. This option is a GNU
extension.

关于bash - 是否可以将 if 语句放入管道中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com