Shell-Script

psgrep 別名以顯示程序 ID 和命令

  • October 21, 2022

我想重寫這個函式,使它只顯示命令名稱和參數,而不必指定所有這些選項awk

function psgrep() 
   # show process id and command with arguments
   ps aux | grep "${1:-.}" | grep -v grep | awk '{print $2, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20}'
}

如果您想搜尋特定的執行檔,ps可以為您做所有事情:

ps -o pid,cmd -C "$1"

如果您想要更一般的搜尋,您可以對以下輸出進行後處理ps -e -o pid,cmd

ps -e -o pid,cmd | awk -v "s=$1" 'NR==1 || $0 ~ s'

引用自:https://unix.stackexchange.com/questions/721853