Write a shell script (to run on the Bourne shell) that uses an infinite loop (i.e. it runs forever unless interrupted by the control-c key sequence) to check every 3 seconds to report on who logs into and who logs out of a UNIX system. To simplify the script, you can ignore multiple logins from the same user during a time period of less than 3 seconds, or cases where a user logs in and logs out during the same 3 second time period.
Specific Task B Requirements
- Your script for this task must be named sh
- Make sure your script is user-friendly and follows common sense – your script should use consistent indentation and include whitespace/blank lines (where relevant) to make the script more logical and easier for a person to read. You must also include general inline comments in the script code to indicate the purpose of more complex command combinations
- Your script must include comments at the start to specify:
- the script’s purpose,
- the author (including student number),
- and the date (when last modified).
- When the script starts executing, it must display the following:
The current time is:
followed by (on the next line) the current date and time e.g.
Wed Sep 1 13:18:32 AEST 2021
- The script must then display a sorted list of usernames of users who are currently logged in:
(using example usernames)
The current users are:
dherbert jsmithe sxu
- Then, every three seconds, the script must display a list of users who have logged in, followed by a list of users who have logged out: (using example usernames)
User abcd has logged in. User jsmith has logged in.
User dherbert has logged out. User jsmithe has logged out.
Continued on next page…
If no user logged in or out during a three second period, then the following message should be displayed:
No user has logged in/out in the last 3 seconds.
Task B Example output
The following is a sample output of the script (It is OK if the script leaves behind a temporary file when it is finally interrupted). The $ is the shell prompt.
Task B Extra Hints:
- Work out how to determine who is logged in – you only want the username part of the output
- Remember the list of users you’ve found above to a file (imagine this is the first list)
- Create a loop that will run every 3 seconds
- Within the loop, determine again who is logged in (saving the username results to another file – imagine this is the second list). Compare this list of usernames with the first list you saved before. Differences between the files indicate usernames who have logged in or out – for each file you may have to use another loop to go through each username in turn in order to determine if it’s present (or not) in the other For example, if a user “bob” is in the first list of usernames but not the second list of usernames, then bob has logged out. If bob is in the second list but not the first list, then bob has logged in. If both lists are exactly the same, then no-one has logged in or out.
- Each iteration of your loop, the second list of usernames from before should become the first list of usernames (they are now old, from 3 seconds in the past) so you can then compare it with a (new) second list of usernames that have just been
- Some users might be logged in multiple times at once, so when you generate a list of who’s logged in, your list should be sorted and only contain unique usernames…