The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bourne: How to invoke an alias from within a shell script techshots Shell Programming and Scripting 2 06-04-2006 12:38 AM
cd from a Bourne Shell Script - Please Help fawqati Shell Programming and Scripting 10 05-25-2006 03:26 AM
Bourne Shell Script dmhonor914 UNIX for Dummies Questions & Answers 2 12-10-2003 12:25 PM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 03:38 PM
Bourne shell script need help please ? dezithug UNIX for Advanced & Expert Users 7 10-24-2001 01:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-08-2006
noodlesoup noodlesoup is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 13
Bourne Shell script - log for users logging on and off

Hello all, I'm new to shell scripting and want to make a script that I can write to log the users logging on and off the a unix system.

I have had a good look over the past few days to crack it, I think I am getting close.

I want a script that runs an infinite loop to check every 5 seconds to report on who logs into and who logs out of the unix system.

This is a example of the output I would like:

PHP Code:
The current users are:
abc1
rv0
sxu
tgray

No user has logged in
/out in the last 5 seconds.

No user has logged in/out in the last 5 seconds.

User rsmith has logged in.

No user has logged in/out in the last 5 seconds.

User abc1 has logged out.

No user has logged in/out in the last 5 seconds
I am using
who | awk '{print $1}' | sort > temp1
to get the users logged on and saving that into a temp file.

and have a while loop set up like this which will run each 5 seconds.

while true
do
.
.
.
.
.
sleep 5
done

The problem I'm having is, I can't work out how to compare the current users logged in to the users that have previously logged in.

I thought of making a second temp file that stores the current users then using:
cmp temp1 temp2
to look at the difference. problem is it gives me an output saying "file differs at character 42, line 6" and that isn't helping me much as I would like to then display that users name but not sure how to print that position in the file (or just a line for that matter).

The test to see if the user has logged on or off is easy, as it is only a couple of if statements.

like if user is in temp1 but not in temp2 then the user has logged out and vice versa.

I have been looking at this for a while now and am going around in circles. any code fragments, help or written shell scripts would be greatly appreciated.

Thanks in advance

Noodle

Last edited by noodlesoup; 09-08-2006 at 05:34 AM..
  #2 (permalink)  
Old 09-08-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Use the 'comm' utility. The -13 and the -23 options will give you what you want. Check the man page for details.
  #3 (permalink)  
Old 09-08-2006
noodlesoup noodlesoup is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 13
you are quite the life saver, thank you very much.

Also is there a quick command you can use for a an if statement to see if the files differ?

if [statement here].

I had a look at the cmp utility and the exit status was 0 is the files were identical and 1 if the files were diferent. but I couldn't get the exit status printed to screen.

Thanks in advance
  #4 (permalink)  
Old 09-08-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Hm. Use the diff command. The exit code for diff is 0 if the files are identical, and 1 if they are not.
  #5 (permalink)  
Old 09-08-2006
noodlesoup noodlesoup is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 13
OK, this is what I have got now, not srue where I have gone wrong. it says I have a syntax error on line 30; ';;' unexpected? any ideas? I thought these were needed for the case to work

PHP Code:
#! /bin/sh

echo "The current users are:"

who awk '{print $1}' sort temp1
cp temp1 temp2
more temp1

while true
do
    
who awk '{print $1}' sort temp2
    cmp 
-s temp1 temp2

    
case "$?" in
    
    0
)
        echo 
"No user has logged in/out in the last 5 seconds."
        
;;
    
    
1)
        
user=`comm -23 temp1 temp2`
        
file=`grep $user temp1 temp2 | cut -c 1-5`

        if [ 
$file "temp1" ]
            echo 
"User "$user" has logged out."
            
        
        
if [ $file "temp2" ]
            echo 
"User "$user" has logged in."
        
;;
        
    
esac
    
    rm temp1
    mv temp2 temp1
    
    sleep 5

done 
  #6 (permalink)  
Old 09-08-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
       if list then list [elif list then list] ... [else list] fi
              If the exit status of the first list is zero, the second list is
              executed; otherwise the list following the elif, if any, is exe-
              cuted with similar consequences.  If all the lists following the
              if and elifs fail (i.e., exit with non-zero  status),  the  list
              following the else is executed.  The exit status of an if state-
              ment is that of non-conditional list that  is  executed;  if  no
              non-conditional list is executed, the exit status is zero.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0