I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made.
- The problem statement, all variables and given/known data:
The task is to create a script which prints information about users whose names are specified in the parameters between spaces.
Result should display:
- user name
- UID
- GID
- home path
- status (logged in or not)
- if yes specify terminal name and number of processes running
If user is logged in to more than one terminal then it should display info about every terminal separately.
If there is no parameter specified then information displayed should concern the user who run the script
-
Relevant commands, code, scripts, algorithms:
I used simple commands:
grep ps who wc cut
to get necessary information -
The attempts at a solution (include all code and scripts):
#!/bin/sh
if [ -z "$*" ]
then
users=$(who)
else
users=$*
fi
for name in $users
do
line=$(grep "^$name:" /etc/passwd)
if [ -z "$line" ]
then
echo "User not found"
else
printf "$line" | cut -f 1,3,4,6 -d:
if [ -n "$(who | grep $name)" ]
then
printf " logged in at "
terminale=$(who | grep $name)
terminale=`echo "$terminale" | cut -f2 -d" "`
for terminal in $terminale
do
printf "%s " $(echo "$terminal" | cut -f2 -d" ")
printf "%s processes " $(ps -t $terminal | wc -l)
done
printf "\n"
else
printf "Not logged in \n"
fi
fi
done
- Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Wojskowa Akademia Techniczna, Poland
Operating Systems (Systemy Operacyjne)
A.Zieliski