# A program that uses the last utility to determine the time that # each student is logged on for the current month # This program can be invoked by passing the students username as # an arguement, which returns the logged time for that student; # or the program can be invoked with no arguments to retrieve a # list of all users' logged time # check to see if the program has been invoked with arguements # if yes, use those username, if no use the entire class list if [ -z "$*" ];then users="`last | sort | sed -e 's/^\([a-z][a-z0-9]*\).*$/\1/' | uniq`" else users="$*" fi # loop through the list to retrieve logged times for n in $users; do days="`last | grep $n | grep -v 'still logged in' | grep -v 'gone - no logout' | grep '+' | sed -e 's/^.*(\(.*\)).*$/\1/' | cut -d':' -f1`" hours="`last | grep $n | grep -v 'still logged in' | grep -v 'gone - no logout' | sed -e 's/^.*(\(.*\)).*$/\1/' | cut -d':' -f1`" minutes="`last | grep $n | grep -v 'still logged in' | grep -v 'gone - no logout'| sed -e 's/^.*(\(.*\)).*$/\1/' | cut -d':' -f2`" # loop through the list of hours, removing leading zeros # and adding the remaining numbers together for h in $hours;do # check for '+' in case there's more than 24 hours chk=`echo $h | grep '+' | wc -l` #totaldays=0 #days=0 if [ $chk -eq 0 ]; then # not more than 24 hours h="`echo $h | sed -e 's/^0\([1-9][0-9]*\)/\1/'`" else # get the number of days, then add it up as d days="`echo $h | cut -d '+' -f1 | sed -e 's/^0\([1-9][0-9]*\)/\1/'`" let totaldays=$totaldays+$days # use the logic above to get h h="`echo $h | cut -d '+' -f2 | sed -e 's/^0\([1-9][0-9]*\)/\1/'`" fi # add the hours extracted to totalhrs let totalhrs=$totalhrs+$h let totaldays=$totaldays+0 done # loop through the list of minutes, removing leading zeros # and adding the remaining numbers together for m in $minutes;do m="`echo $m | sed -e 's/^0\([1-9][0-9]*\)/\1/'`" let totalmin=$totalmin+$m done # devide totalminutes by 60 to extract the number of hours # add hours extracted to hrs and leave the remaining minutes in min hrs=$(($totalmin/60)) min=$(($totalmin-$(($hrs*60)))) # add totalhrs to hrs hrs=$(($hrs+$totalhrs)) # devide hrs by 24 to extract days from hrs, then add totaldays to get final number of days days=$(($hrs/24)) hrs=$(($hrs-$(($days*24)))) days=$(($days+$totaldays)) for ((nn=0; nn<$days; nn++ ));do dbar=$dbar"@" done for ((nnn=0; nnn<$hrs; nnn++)); do hbar=$hbar"#" done for ((nnnn=0; nnnn<$min; nnnn=nnnn+10 )); do mbar=$mbar"." done echo "-----------------+------+" bar="$dbar $hbar $mbar" printf " %-8s | %-2s : %-2s : %-2s | %-2s %-2s %-2s \n" $n $days $hrs $min $dbar $hbar $mbar #echo "$n | $days days : $hrs hours : $min min | $dbar $hbar $mbar" #echo "$n | $h" bar="" hbar="" dbar="" mbar="" totalmin=0 totalhrs=0 totaldays=0 days=0 hrs=0 min=0 done