gpt4 book ai didi

linux - 无论 PID 结果如何,当从 ps 管道传输到 grep 时,Bash 脚本输出始终为 'ps'

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:52 26 4
gpt4 key购买 nike

给定一个 pid 数组和代码:

    for i in ${listedPids[@]}
do
runningCheck="ps -u $USER | grep $i"
grepRes=(${runningCheck})
if [[ -n $grepRes ]]
then
echo $grepRes
echo $runningCheck
... code not related to the issue
fi
done

无论这些 pid 是否处于事件状态;我一直从 echo $grepRes 获取“ps”,而 echo $runningCheck 的输出显示正确的用户名和 pid。我错过了什么?

最佳答案

替换

"ps -u $USER | grep $i"

通过

$(ps -u $USER | grep $i)

Command Substitution: Bash performs the expansion by executing your command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

关于linux - 无论 PID 结果如何,当从 ps 管道传输到 grep 时,Bash 脚本输出始终为 'ps',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925295/

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