문제

I think I may have approached this problem the wrong way and I could really use a hand here. I'm trying to print a report to the screen using awk. I want to list the users logged in and their full names next to each user. The only way I could figure it out is below, but it only shows my own info. Can I add it into a loop somehow to achieve this or did I go about it completely wrong?

This is what I have:

echo "User name"  "|"  "Full name"
echo "--------------------------"
echo -n "$USER " ; awk -v user="$USER" -F":" 'user==$1{print$5}' /etc/passwd
도움이 되었습니까?

해결책

The $USER variable just contains your username.

You can pipe the who command to get the list of logged in users.

echo "User name"  "|"  "Full name"
echo "--------------------------"
who | while read username rest; do
    echo -n "$username " ; awk -v user="$username" -F":" 'user==$1{print$5}' /etc/passwd
done

다른 팁

Whenever you find yourself writing a loop in shell to process text, you have the wrong solution.

who | awk '
BEGIN { print "User name|Full name\n--------------------------" }
NR==FNR { name[$1] = $5; next }
{ print $1, name[$1] }
' FS=":" /etc/passwd FS=" " -

Shell is just an environment from which to call tools and it has a language to sequence those calls. The shell tool to process text is awk.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top