我想查找執行特定命令作爲參數的用戶的所有名稱。 必須使用grep。 我試過了:ps aux | grep $ 1 |砍掉-d「」-f1,但這不是理想的結果。所有用戶執行某個命令。 grep
0
A
回答
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
我猜你正在尋找這一點。
# 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
。
相關問題
- 1. 如果對某個用戶執行命令
- 2. grep命令行,linux
- 3. J.S不執行所有命令
- 4. 如何記錄所有Unix用戶在文件中執行的所有命令?
- 5. 多行grep命令(不是多行grep)
- 6. 多個命令用grep
- 7. 如何使用「grep」命令列出用戶在當前目錄下可執行的所有文件?
- 8. 如何在啓動時執行其他所有命令後執行命令?
- 9. 有沒有辦法列出用戶可以執行的所有命令(KSH)?
- 10. vb doens't執行所有批處理命令,只有幾個
- 11. 如何grep和執行命令(對於每一個匹配)
- 12. 在cmd中執行幾個命令。執行只有一個命令
- 13. 無法運行grep命令
- 14. gradle這個:常規命令行 - grep的
- 15. 用PHP執行Asterisk客戶端命令
- 16. 用戶無法執行命令
- 17. 執行非root用戶命令
- 18. 閱讀用戶命令並執行它
- 19. 蟒蛇執行多個命令子
- 20. 這個grep命令有什麼問題?
- 21. 如何在後臺使用bash腳本執行grep鏈命令
- 22. 當grep找不到匹配項時,使用grep命令執行subprocess.check_output失敗
- 23. Bash腳本沒有執行命令
- 24. postgresql命令沒有執行
- 25. WPF,命令沒有執行
- 26. Unix grep命令
- 27. Grep命令
- 28. Grep sed命令
- 29. 在grep命令
- 30. UNIX grep命令
這不是100%準確,但可能是OP – Kent
@sarathi的好開始,它還包括執行/ usr/ucb/ps aux | awk'//{print $ 1}'| sort -u,但他沒有執行作爲參數給出的命令。 –