2013-03-28 14 views

回答

0
/usr/ucb/ps aux | awk '/<your_command_as_parameter>/{print $1}'|sort -u 

爲如:

> /usr/ucb/ps aux | awk '/rlogin/{print $1}' | sort -u 
+0

這不是100%準確,但可能是OP – Kent

+0

@sarathi的好開始,它還包括執行/ usr/ucb/ps aux | awk'/ /{print $ 1}'| sort -u,但他沒有執行作爲參數給出的命令。 –

0

我猜你正在尋找這一點。

# cat test.sh 
ps aux | grep $1 | grep -v grep | awk '{print $1}' 
# ./test.sh bash 
root 
root 
root 
+0

除非您正在尋找運行'grep'的人,否則這些工作主要是有效的。 –

0

有一種技巧可以獲取進程的信息,但不是搜索進程的進程,即將該進程的名稱變爲正則表達式。例如,如果您要搜索ls,請將搜索字詞設爲grep '[l]s'。除非您正在搜索grep本身或一個單字母命令名稱,否則這種方法可行。

這是我使用的procname腳本;它的工作原理與大多數POSIX殼:

#! /bin/ksh 
# 
# @(#)$Id: procname.sh,v 1.3 2008/12/16 07:25:10 jleffler Exp $ 
# 
# List processes with given name, avoiding the search program itself. 
# 
# If you ask it to list 'ps', it will list the ps used as part of this 
# script; if you ask it to list 'grep', it will list the grep used as 
# part of this process. There isn't a sensible way to avoid this. On 
# the other hand, if you ask it to list httpd, it won't list the grep 
# for httpd. Beware metacharacters in the first position of the 
# process name. 

case "$#" in 
1) 
    x=$(expr "$1" : '\(.\).*') 
    y=$(expr "$1" : '.\(.*\)') 
    ps -ef | grep "[$x]$y" 
    ;; 
*) 
    echo "Usage: $0 process" 1>&2 
    exit 1 
    ;; 
esac 

bash,你可以使用變量substringing操作,以避免expr命令:

case "$#" in 
1) ps -ef | grep "[${1:0:1}]${1:1}" 
    ;; 
*) 
    echo "Usage: $0 process" 1>&2 
    exit 1 
    ;; 
esac 

的這兩種運行ps -ef;如果您願意,可以使用ps aux。搜索「命令」名稱並不侷限於命令的命令部分,因此您可以使用procname root來查找由root運行的進程。這場比賽也不受限於一個完整的單詞;你可以考慮使用grep -w(擴展名爲GNU grep)。

這些的輸出是來自ps的全部數據行;如果您只想要用戶(第一個字段),則將輸出傳送到awk '{print $1}' | sort -u