Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Bash中按名称获取正在运行的进程数_Bash_Macos_Terminal - Fatal编程技术网

在Bash中按名称获取正在运行的进程数

在Bash中按名称获取正在运行的进程数,bash,macos,terminal,Bash,Macos,Terminal,假设我在终端中运行以下操作: pgrep Google Chrome 并生成所有PID的以下输出: 110 311 142 我怎样才能准确地显示列出了多少个流程,而不将我自己计算在内,从而产生如下结果: 110 311 142 There are currently 3 processes running under the application 'Google Chrome' 要以指定的格式生成输出,请执行以下操作: proc="Google Chrome" p

假设我在终端中运行以下操作:

pgrep Google Chrome
并生成所有PID的以下输出:

110
311
142
我怎样才能准确地显示列出了多少个流程,而不将我自己计算在内,从而产生如下结果:

110
311
142

There are currently 3 processes running under the application 'Google Chrome'

要以指定的格式生成输出,请执行以下操作:

proc="Google Chrome"

pids=$(pgrep "$proc")
if [[ -n $pids ]]; then
    printf "%s\n\nThere are currently %d processes running under the application '%s'\n" \
        "$pids" "$(wc -l <<< "$pids")" "$proc"
fi
proc=“谷歌浏览器”
pids=$(pgrep“$proc”)
如果[[-n$pids]];然后
printf“%s\n\n当前有%d个进程在应用程序“%s”下运行\n\

“$pids”“$(wc-l我发现包装器函数对这类事情非常有用:

pgrep(){
本地应用程序=${!#}pids
#现在调用*命令*pgrep并
#将输出捕获到一个行数组中

readarray-t pids
pgrep'Google Chrome'| wc-l
top | grep Google Chrome | uniq类似这样的wokrs?