Link to home
Start Free TrialLog in
Avatar of sriram_bandi
sriram_bandi

asked on

shell program

write a shell program(boune shell) on the following topic.

watching all people logging in and out and report as people come and go- a sort of incremental who. also display statistics of people logged in and logged out every 2 minutes.
Avatar of jonke
jonke

Is this the sort of thing you are after?

#!/bin/ksh
#
#Lets see who is on the system at the moment
#
clear
echo "USERS WHO ARE CURRENTLY ON THE SYSTEM"
echo ""
who
who > /tmp/who1
#
#Create an eternal loop
#
LOOP=1
while (( $LOOP == 1 ))
        do
        sleep 20
#
#Make it all nice and neat
#
        clear
        echo "USERS WHO ARE CURRENTLY ON THE SYSTEM"
        echo ""
        who
        who > /tmp/who2
#
#Compare who output, with the one from 2 mins ago
#
        diff /tmp/who1 /tmp/who2 > /tmp/who_diff
        grep ">" /tmp/who_diff > /tmp/who_joined
        grep "<" /tmp/who_diff > /tmp/who_left
#
#Then output who is currently on the system, who has left and who has joined
#in a presentable format
#
        echo ""
        echo "USERS THAT HAVE LOGGED ONTO THE SYSTEM"
        echo ""
                if [[ -s /tmp/who_joined ]]
                then
                cat /tmp/who_joined
                else
                echo "No users have logged onto the system in the last 2 mins"
                fi
Sorry- you wanted bourne shell didn't you?

Here is the revised version:

#!/bin/sh
#
#Lets see who is on the system st the moment
#
clear
echo "USERS WHO ARE CURRENTLY ON THE SYSTEM"
echo ""
who
who > /tmp/who1
#
#Create an eternal loop
#
while [ 1 ]
        do
        sleep 120
#
#Make it all nice and neat
#
        clear
        echo "USERS WHO ARE CURRENTLY ON THE SYSTEM"
        echo ""
        who
        who > /tmp/who2
#
#Compare who output, with the one from 2 mins ago
#
        diff /tmp/who1 /tmp/who2 > /tmp/who_diff
        grep ">" /tmp/who_diff > /tmp/who_joined
        grep "<" /tmp/who_diff > /tmp/who_left
#
#Then output who is currently on the system, who has left and who has joined
#in a presentable format
#
        echo ""
        echo "USERS THAT HAVE LOGGED ONTO THE SYSTEM"
        echo ""
                if [ -s /tmp/who_joined ]
                then
                cat /tmp/who_joined
                else
                echo "No users have logged onto the system in the last 2 mins"
                fi
        echo ""
        echo "USERS THAT HAVE LEFT THE SYSTEM"
        echo ""
                if [ -s /tmp/who_left ]

then
                cat /tmp/who_left
                else
                echo "No users have logged off the system in the last 2 mins"
                fi
        echo ""
        cp /tmp/who2 /tmp/who1
        rm /tmp/who_joined /tmp/who_left
done
ASKER CERTIFIED SOLUTION
Avatar of jonke
jonke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sriram_bandi

ASKER

Thanks a lot jonke... I executed the program it is working good.