2016-09-28 167 views

回答

3

你幾乎沒有。使用getent解決UID到用戶名:

getent passwd `grep "^Uid" /proc/$i/status |awk '{printf "%4s",$2}'`| cut -d: -f1 
+0

謝謝,但也打印數字,我怎麼做纔打印用戶名? –

+0

只爲我打印用戶名。 – Misko

0
% getent passwd $(grep -oP '^Uid:\s*\K\d+' /proc/$$/status) | cut -d: -f1 

變化$$(當前的shell突未PID)

+0

你看了這個問題嗎?他想要的是用戶名,而不是UID。 – Barmar

+0

標題只是一個總結。實際的問題在文中。你必須閱讀整個事情。 – Barmar

0
$ awk 'NR==FNR {uid[$3]=$1; next} /^Uid:/ {print uid[$2]}' FS=":" /etc/passwd FS="[ \t]+" /proc/1665/status 
root 

解釋:

NR==FNR {   # /etc/passwd 
    uid[$3]=$1 # an array with key=uid, value=username 
    next 
} 
/^Uid:/ {   # /proc/.../status record startin with Uid 
    print uid[$2] # print username from array where uid matches 
}