Question

Please type your answer, thank you! :) The subject is UNIX Using the BASH for and...

Please type your answer, thank you! :)

The subject is UNIX

Using the BASH for and while loops (Textbook question 13.19 page 403)

Write a script that checks each minute and reports on who logs in and who logs out.

You can follow the steps below (not the only way):

  1. Using the commands who and cut, extract the list of usernames currently logged in the system
  2. To check after a minute, the sleep command can be used: sleep 60
  3. Get the new list of users logged in after a minute
  4. For each user in the list;
    1. check if he is in the second list using if and grep. You can dump the output of grep to /dev/null when using it in the as a test for the if statement
    2. If the user is not in the second list, print that the user has logged out
  5. For each user in the second list,
    1. If he is in the first list, print that the user has logged in
  6. Rename the second list as the first list
  7. Repeat the process from step 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

The command shows the list of users logged into the system

$who -u

The command creates a new file for all the usernames. The user name is the first field of the etc/password file.The cut command extracts the list and stores the sorted list in a file file1

$ cut -d: -f1 /etc/passwd | sort > file1

Sleep for 60 secs

$sleep 60

Get the list of new logged in uses after 60 secs and stores the sorted list of users in another file file2

$ cut -d: -f1 /etc/passwd | sort > file2

Now we shall compare both the files . if user is in file 1 but not in file 2 it means user has logged out . The following command reports the list of users who are present in file 1 but not present ie absent in file 2 meaning logged out hence we should print user has logged out .

diff -u file2 file1 | grep " ^+"

We can also use the bash short cut

diff < file1 <file2

However for the specific purpose sought in the question we shall use the comm command

comm [option] file 1 file 2

for options

-1 suppress lines unique to file 1

-2 suppress lines unique to file 2

-3 suppress lines common to both files

comm -2 -3 < file1<file2 >file3

This will output users who were initially logged in but later logged out

Now we run a loop to display users who have logged out

for i in 'file3'; do

echo $i "has logged out"

done

wait

Similarly we can check for user present in second list but not in first list and for each user print "<user> has logged in "

comm -1 -3 < file1<file2 >file4

for j in 'file4'; do

echo $j "has logged in"

done

wait

Now rename the second file

mv file2 file1

Then we can enter into the loop for step 2

 
Add a comment
Know the answer?
Add Answer to:
Please type your answer, thank you! :) The subject is UNIX Using the BASH for and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In UNIX Write a script that checks each minute and reports on who logs in and...

    In UNIX Write a script that checks each minute and reports on who logs in and who logs out. You can follow the steps below (not the only way): Using the commands who and cut, extract the list of usernames currently logged in the system To check after a minute, the sleep command can be used: sleep 60 Get the new list of users logged in after a minute For each user in the list; check if he is in...

  • I need this done ASAP. Please write a bash shell script that does the following: Prints...

    I need this done ASAP. Please write a bash shell script that does the following: Prints out how many users are logged on. This can be accomplished using who and wc. Prints out a list of currently logged on users. This can be accomplished using who, grep, regular expressions, and echo. Prints out how many processes you have running from past days, and a list of those jobs. Use `whoami` to get your username. Run ps -ef and see how...

  • These are the questions in intro to os and unix. Your efforts are appreciated. Thank you...

    These are the questions in intro to os and unix. Your efforts are appreciated. Thank you very much ! q1) [2] Give an abstract, high-level description of what a computer is. q2) [5] List the elements of a Von Neumann Machine. q3) [3] Name the two types of software, and give a brief description of each. q4) [2] What are the 2 Roles/Purposes of the OS? q5) [5] What are the OS's management tasks? q6) [1] What is a process?...

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • INFO 1211

    Lab 0: Essential UNIX OperationsObjectivesAt the end of this of lab, you should be able to:securely      log in to a remote computer running a UNIX-like operating systemread the      manual page for any commanduse the      UNIX file systemedit a      text fileNotes:·         In order to get familiar with UNIX, do all your work on UNIX.·         A command line interface may be harder to learn, but can be more powerful for scripting and automating tasks.·        ...

  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

  • Mini Project You are required to do this assignment on your own skills. No copying from...

    Mini Project You are required to do this assignment on your own skills. No copying from other students are not allowed. Write a menu driven Bash script using the guidelines given below. Some of the commands and syntax may not be covered in lectures. Please refer other sources to understand syntax and commands you need to complete this assignment. This program takes user input and to perform simple arithmetic operations such as addition, subtraction, multiplication and division of any two...

  • Python 3 Problem: I hope you can help with this please answer the problem using python...

    Python 3 Problem: I hope you can help with this please answer the problem using python 3. Thanks! Code the program below . The program must contain and use a main function that is called inside of: If __name__ == “__main__”: Create the abstract base class Vehicle with the following attributes: Variables Methods Manufacturer Model Wheels TypeOfVehicle Seats printDetails() - ABC checkInfo(**kwargs) The methods with ABC next to them should be abstracted and overloaded in the child class Create three...

  • UNIX File Permission help, please answer the questions in the green boxes. Thank you Lab 03...

    UNIX File Permission help, please answer the questions in the green boxes. Thank you Lab 03 File Permissions In this lab we will: learn about file permissions learn to create symbolic links and hard links Utilities that will be utilized in this Lab: us, cd, less, cat touch, chmod id umask, mkdir, In, echo and redirection Users and Groups Linux supports several methods of controlling access to files an directories. In this lab we are going to learn the traditional...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT