1844295 Members
3132 Online
110230 Solutions
New Discussion

Shell Script

 
SOLVED
Go to solution
Aggy
Frequent Advisor

Shell Script

I am writing a script to get yesterdays users who are logged in.
The script works fine but need to make few changes to the script so that it echo “No users Yesterdays users logged in” if it finds no user from yesterday logged and should display yesterdays users if they are logged in

See example below:

Check if yesterdays users still logged
=============================================================
gmt1 pts/7 Sep 25 09:58 0:37 24165 dev1:1.0

Check if any critical errors in syslog
=============================================================

and I get this O/P when no users logged in and no errors

Check if yesterdays users still logged
=============================================================
Check if any critical errors in syslog
=============================================================

How can I make changes to the script so that if no errors then it should display as below

Check if yesterdays users still logged
=============================================================
No yesterdays users logged

Check if any crucial erros in syslog
=============================================================
No critical errors logged

Part of my script as below:

#This function checks if yesterdays users were killed by OPS
function check_yesterday_users
{
echo >> $LOGFILE
echo "Check if yesterdays users still logged" >> $LOGFILE
echo "===============================" >> $LOGFILE
echo >> $LOGFILE
who -u | grep "$1" >> $LOGFILE
}

3 REPLIES 3
OldSchool
Honored Contributor
Solution

Re: Shell Script

echo >> $LOGFILE
echo "Check if yesterdays users still logged" >> $LOGFILE
echo "===============================" >> $LOGFILE
echo >> $LOGFILE
who -u | grep "$1" >> $LOGFILE

add immediately after the line above:

if [ $? = 1 ]
then
echo "No yesterdays users still logged" >> $LOGFILE
fi


the grep will return 1 if no matches found, 0 if they are. similar logic applys to the syslog error piece



A. Clay Stephenson
Acclaimed Contributor

Re: Shell Script

I am not going to bother to answer your specific questions because there is a fundamental flaw in your design. You are relying upon the "who" command to tell you who is on the system. Regardless of the UNIX flavor, who is the system's "best guess" as to who is actually on the system. Using who, your script will work well almost all the time --- which is the worst kind of problem to debug. Sure you can use fwtmp to fix utmp when users are not logged off cleanly but you are really just fixing the symptoms of the problem. Rather than using who, use the process table (ps command) because it is reliable and trustworthy.
If it ain't broke, I can fix that.
Aggy
Frequent Advisor

Re: Shell Script

Thanks