I have a working script that tells how long as user has been logged on . But it the user isn't logged on it will say they are logged off but it will give me an error for one of the tests in the middle of the script.
Im trying to figure out how to insert the following code in the rest of my script so it skips it if the user isn;t online.
if [ $eMin -lt 0 ]
then
eMin=$(expr $eMin + 60 )
eHr=$(expr $eHr - 1 )
fi
The full script for xontext is as follows, it works just returns an error message for the math part if the user isn't online.
while [ -z "$1" ]
do
echo -n "Please enter valid id: "
read var1
set $var1 $1
# Break the loop
if [ -n "$1" ]
then
break
fi
done
# Check if the user is a valid user
if id "$1" >/dev/null 2>1;
# If so, then check if he if he is currently logged on and set on/not on as variable
then
# Set full name variable based on matching ID
fullname=$(grep "$1" /etc/passwd | cut -d ':' -f5 | sort -k 2 | tr ",,:" " " | awk '{print $2,$1}')
# Get Current Hours and Minutes
nowHr=$(date | cut -c 12,13)
nowMin=$(date | cut -c 15,16)
onHr=$(who |grep "$1" | cut -c 34,35)
onMin=$(who |grep "$1" |cut -c 37,38)
# Hours minutes spent logged on
eHr=$(expr $nowHr - $onHr )
eMin=$(expr $nowMin - $onMin )
if [ $eMin -lt 0 ]
then
eMin=$(expr $eMin + 60 )
eHr=$(expr $eHr - 1 )
fi
# Test and display user name and if curretnly logged on
# Exit with code of 0 if success and 1 if fail
who -u |grep -q "$1" || test && echo "$fullname is logged on for $eHr hour(s) and $eMin minutes(s)." && exit 0 || echo "$fullname is not logged on" && exit 1
# Displays if invalid id is entered
else
echo "The user you entered, $1 is not a valid user on this system"
# Exit with code of 2
fi
exit 2
The output when the user isn't online is
expr: syntax error
expr: syntax error
/timeOn: line 38: [: -lt: unary operator expected
John Doe is not logged on
Aucun commentaire:
Enregistrer un commentaire