簡體   English   中英

編寫腳本以查找用戶進程的所有PID

[英]Writing script to find all PID of processes of user

我真的很想用ps -aux來得到這個。 所以我寫了腳本:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read uid
else
        uid=$1
fi
procesy=`ps -aux | awk '{if ($1=="$uid") print$2}'`
echo $procesy

為什么它不起作用? 當我寫./script root時,除了空行外,我什么都沒得到。

這將滿足您的需求:

pgrep -U $1

在腳本中,您在awk內使用shell變量時遇到問題。 使用awk的正確方法應該是:

#!/bin/bash
if [ $# -lt 1 ]; then
        echo -n "No arguments were written"
        read user
else
        user=$1
fi
procesy=`ps aux | awk -v usr=$user '{if ($1==usr) print$2}'`
echo $procesy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM