I received this linux script from my professor and I was trying to understand what it does and I think I know what most of it does. However, there is one part that confuses me. What does junk
do in this line:
who | while read user junk
What's its purpose? I've tried googling it but I just can't seem to find it anywhere other than referencing junk characters and from what I can tell that's a separate thing. The whole script is:
#!/bin/bash
#Run a while loop using input of user command
who | while read user junk
do
#Get users real name from /etc/passwd file
realname=`grep $user /etc/passwd | cut -d: -f5`
echo "User: $realname ($user)"
#Get user's logged in time using who command
loggedin=`who | grep $user | awk '{print $3" "$4}'`
echo "Logged in: $loggedin"
#Get home directory of the user from /etc/passwd file
homedir=`cat /etc/passwd | grep $user | cut -d":" -f6`
echo "Home Directory is $homedir"
#Get count of all files in user's home directory
#including sub-directories (excluding directory count) using find command
file_dir_count=`find $homedir -type f | wc -l`
echo "Home directory contains $file_dir_count files"
#Get user's processes count using ps command
proc_count=`ps -u $user --no-headers | wc -l`
echo "$user has $proc_count processes"
#Display user's top 7 memory using process list in given order using top
echo "Top 7 processes sorted by Memory Usage:"
top -b -n 1 -u $user -o %MEM | head -14 | tail -8 | awk '!($3="")'
done
junk
is a variable name. If you enterfoo bar baz
,read user junk
will assignfoo
touser
andbar baz
tojunk
. He probably called itjunk
because he ignores its value, and cares only about the first word entereded by the user. As to the rest of the script -- find a better professor ;-) – Apr 18 '20 at 16:44