dimanche 21 avril 2019

Can't get echo to output message at the end of script

I have a script that looks at a user and tell you how long the user is online. but at the end I can't make it output the message the user isn't a valid user.

If they arn't a valid user then noting is out put.

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

while id "$1" >/dev/null 2>1;
do
lUser=$(who | grep "$1" | awk '{ print $1 }')

if [ -n "$lUser" ]
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

echo "$fullname is logged on for $eHr hour(s) and $eMin minutes(s)."
break

elif [ -z "$lUser" ]
then
if id "$1" >/dev/null 2>1;
then
fullname=$(grep "$1" /etc/passwd | cut -d ':' -f5 | sort -k 2 | tr ",,:" " " | awk '{print $2,$1}')
echo "$fullname is not logged on"

else
echo "$1 is not a valid user"
fi





fi

done

The line with the problem is the

echo "$1 is not a valid user"

The rest works if the user is valid, but if not then it doesn't out put a message.

Aucun commentaire:

Enregistrer un commentaire