In: Computer Science
Write a shell script that will create a new password-type file that contains user information. The file should contain information about the last five users added to the system with each line describing a single user. This information is contained in the last five lines of both the /etc/shadow and the /etc/passwd files. Specifically, you should produce a file where each line for a user contains the following fields: user name, encrypted password, and shell. The fields should be separated using the colon (:) character.
Please find the below shell script for the given scenario. Please copy paste the below script in a file with extension .sh( for example test.sh) and change the permission of the file(add execute permission) using chamod command as shown below and execute the filr as ./test.sh
chmod 755 test.sh
Sample entry of the /etc/passwd files is as follows( here the first column is the user name and the seventh column is users shell )
tom1:x:1000:1000:Vivek Gite:/home/vivek:/bin/bash
Sample entry in /etc/shadow file is as follows( here the first column is the user name and the second column is encrrypted password)
linuxize1:$6$zHvrJMa5Y690smbQ$z5zdL...:18009:0:120:7:14::
So to get the desired output as mentioned in the question, ie 'username:encrypted password:shell of the user' we need to extract the first column(user name [-f 2]) from /etc/passwd using cut command and delimiter as colon (-d:). Then extract the second column(encrypted password [-f 2]) from the /etc/shadow using cut command and delimiter as colon (-d:) . Then extract the 7th column (user shell [-f 7]) from /etc/passwd using cut command and delimiter as colon (-d:).
After extracting each columns, the concatenate all these fileds together using the 'paste' command with colon(:) as delimiter to get the final output. Then write this output to a file with name 'newpass.txt'
The 'tail -n' command is used to get the last 'n' lines from a file. So 'tail -5 /etc/passwd' gives the last 5 lined of the file /etc/passwd.
Please provide your feedback.
Thanks, Happy Learning!
#!/bin/bash
paste --delimiter=':' <(tail -5 /etc/passwd | cut -d: -f 1) <(tail -5 /etc/shadow | cut -d: -f 2) <(tail -5 /etc/passwd | cut -d: -f 7) > newpass.txt