Display info about users (UID GID processes terminal)

I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made.

  1. 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

  1. Relevant commands, code, scripts, algorithms:
    I used simple commands:
    grep ps who wc cut
    to get necessary information

  2. 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
  1. 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

Some comments, as requested:

  • Don't use who if parameters are missing (too many fields); use whoami .
  • Use a format string for the first printf , and consider abandoning echo in printf 's favour.
  • Don't use $(who | grep $name) several times; run it once and store its output.
  • Use `...` or $(...) consistently.
  • Due to possibly different output fields between your and my who , I can't comment on the terminal(e) stuff, but with my who 's results that processing looks bizarre

printf "$line" would do special actions on % signs. Better have "%s" as you did it elsewhere.
who | grep "$name" should match the whole word at the beginning. You did it correctly with /etc/passwd.