#!/usr/local/bin/bash #The dialog tool to create our user interface needs a temp file #that it can write output to DIALOG_TEMP_FILE="/tmp/week7.dialog.tempfile" #Grab a list of all the usernames in the password file #The second grep excludes user who have just / for their home directory usernamesList=`cat /etc/passwd | grep -v "^#" | cut -d":" -f1,6 | grep -v "/$" | cut -d":" -f1` #Assemble a list of users for the dialog command #This will look like this # 1 danielt 2 wharrop 3 mdieker userlist="" counter=0 for username in $usernamesList do counter="$((counter + 1))" userlist="$userlist $counter $username" done #Show a menu with an item for each user we found above #The user will have to select an item from this list menuWidth=30 menuHeight=30 menuItems=20 dialog --menu "Available Users" $menuHeight $menuWidth $menuItems $userlist 2> $DIALOG_TEMP_FILE if [ $? = 1 ]; then clear exit 0 fi itemSelected=`cat ${DIALOG_TEMP_FILE}` #Work out which user this corresponded to #Do this by getting the Nth line of the userlist usernameSelected=`echo "${usernamesList}" | head -n ${itemSelected} | tail -n 1 ` #Work out the other bits of information we're supposed to show #We grab only the first 20 running processes to simplify our output usersRunningPrograms=`ps -o"pid command" -U $usernameSelected | head -n 20` lastLoginDate=`last -n 1 $usernameSelected | head -1 | tr -s '\t' ' ' | tr -s ' ' ' ' | cut -d' ' -f4-7` usersHomeDirectory=`cat /etc/passwd | grep "^${usernameSelected}" | cut -d":" -f6` sizeOfHomeDirectory=`du -ckh -d0 $usersHomeDirectory | grep "total" ` #Assemble this information into a big string and show it in an info box usersInformation="You chose: ${usernameSelected} \n" usersInformation="${usersInformation}Last Login: ${lastLoginDate} \n" usersInformation="${usersInformation}Home Dir Size: ${sizeOfHomeDirectory} \n" usersInformation="${usersInformation}Running Programs:\n\n ${usersRunningPrograms} " infoBoxWidth=80 infoBoxHeight=30 dialog --msgbox "${usersInformation}" $infoBoxHeight $infoBoxWidth #Re run ourselves #HACK! $0