1. The problem statement, all variables and given/known data:
If the user is not currently on, the script will:
a) issue a message using the users name in first name last name order and indicating that
the user is not on
b) end the script with a code of 1.
If user is currently on, the script will:
a) the same message but it will indicate:
- that the user is on
- which terminal the user is using
- how long the user has been on (will not exceed 24 hours).
b) end the script with a code of 0
3. The attempts at a solution (include all code and scripts):
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Bristol Community College, Fall River, Mass, US, Professor Silvino Ferreira, CIS132 Intro to unix (it wont let me post a link to his site because im a brand new member)
the main part of my code works perfectly, i just dont know how to do the while statement or im not sure if i should be using an elif statement or how that would even work, just looking for a step in the right direction because i'm getting really frustrated
---------- Post updated at 07:11 PM ---------- Previous update was at 05:20 PM ----------
okay so i changed it a little bit, but it still does not show the variables in my echo
Quote:
echo -n "please enter a valid user ID "
read -e a
er=$(who | grep $a | cut -c30,31)
me=$(who | grep $a | cut -c33,34)
ur=$(date | cut -c12,13)
my=$(date | cut -c15,16)
who | grep "$a" > /dev/null
if [ "$?" -eq 1 ]
then
sleep 1
echo "$a is not online"
elif [ "$?" -eq 0 ]
then
if [ "$ur" -ge "$er" ]
then
hrs=$(($ur - $er))
else
hrs=$((($er - $ur) + 24))
if [ "$my" -gt "$me" ]
then
min=$(($my - $me))
else
min=$((60 - ($me - $my)))
fi
fi
fi
echo "`cat /etc/passwd | grep $a | cut -d: -f1,5 | sed '1,$s/\(.*\):\(.*\),\(.*\)/\1 \3 \2/' | sort -k 3` has been logged in for a "$hrs" hours and "$min" minutes.";
it seems like it doesn't do anything in between other than the two if statements
---------- Post updated at 08:09 PM ---------- Previous update was at 07:11 PM ----------
OKAY i think it works
Quote:
echo -n "please enter a valid user ID "
read -e a
er=$(who | grep $a | cut -c30,31)
me=$(who | grep $a | cut -c33,34)
ur=$(date | cut -c12,13)
my=$(date | cut -c15,16)
who | grep "$a" > /dev/null
if [ "$?" -eq 1 ]
then
sleep 1
echo "$a is not online"
exit 1
fi
who | grep "$a" > /dev/null
if [ "$?" -eq 0 ]
then
if [ "$ur" -ge "$er" ]
then
hrs=$(($ur - $er))
else
hrs=$((($er - $ur) + 24))
if [ "$my" -gt "$me" ]
then
min=$(($my - $me))
else
min=$((60 - ($me - $my)))
fi
fi
echo "`cat /etc/passwd | grep "$a" | cut -d: -f1,5 | sed '1,$s/\(.*\):\(.*\),\(.*\)/\1 \3 \2/' | sort -k 3` has been logged in for "$hrs" hour(s) and "$min" minute(s).";
exit 0
fi
can someone confirm or deny this?
Last edited by nick_smizzle; 04-24-2011 at 08:53 PM..
Reason: Changed quote tags to code tags; added indentation
Well, you might put the bulky bits in a variable set not inline, and call who once storing it in a variable who=$(who) . . . echo "$who"|grep. Also, the who set does not change under your feet. cat file | grep ... is silly, grep ... file and save a fork, exec and pipe. cut | sed is silly, use sed. You need to be tigher in your /etc/passwd compares, in case one id is a prefix of another, e.g., for xy get xy, xyz, zxy, zxyz. Use date "+%Y" and the like not cut on date default. Indent commands in containers like if, and stand them off a line from one line command lists.
Last edited by DGPickett; 04-28-2011 at 03:00 PM..
ahh thanks for the tips, it works perfectly now, i also need to loop it with a while statement to do it for people at are on in the class other than just one person, any pointers on that one?