Skip to content

Scripting

VBrazhnik edited this page Jun 28, 2018 · 11 revisions

1. Write a script which displays only the login, UID and Path of each entry of the /etc/passwd file.

Fields in /etc/passwd

Understanding /etc/passwd File Format:

  1. Username: It is used when user logs in. It should be between 1 and 32 characters in length.
  2. Password: An x character indicates that encrypted password is stored in /etc/shadow file. Please note that you need to use the passwd command to computes the hash of a password typed at the CLI or to store/update the hash of the password in /etc/shadow file.
  3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
  4. Group ID (GID): The primary group ID (stored in /etc/group file)
  5. User ID Info: The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc. This field use by finger command.
  6. Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
  7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.

How to write bash script

Writing Your First Script And Getting It To Work:

#!/bin/bash
# My first script

echo "Hello World!"

The first line of the script is important. This is a special clue, called a shebang, given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as Perl, awk, tcl, Tk, and python also use this mechanism.

The second line is a comment. Everything that appears after a "#" symbol is ignored by bash.

Answer

#!/bin/bash
# Write a script which displays only the login, UID and Path

IFS=':'

while read username password uid gid comment home_directory shell
do
    echo "$username:$uid:$home_directory"
done < /etc/passwd

Explanation

read Man Page - Bash:

One line is read from the standard input, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name.

If there are fewer words read from the standard input than names, the remaining names are assigned empty values.

The characters in the value of the IFS variable are used to split the line into words.

Alternative answer

#!/bin/bash
# Write a script which displays only the login, UID and Path

awk -F: '{print $1 ":" $3 ":" $6}' /etc/passwd

Explanation

man awk:

-F fs, --field-separator fs — Use fs for the input field separator (the value of the FS predefined variable).

2. Write a script which updates all the package sources, then all the packages, and then logs everything in a file named /var/log/update_script.log. Create a scheduled task for this script, once per week at 4 AM.

#!/bin/bash
# Write a script which updates all the package sources, then all the packages, and then logs everything in a file named /var/log/update_script.log
# Path of script file is /root/update.sh
# To schedule this task add entry '0 4 * * 1 /root/update.sh' in cron file. To do it you need to execute command crontab -e

LOG_FILE=/var/log/update_script.log

apt-get update >> $LOG_FILE
apt-get upgrade >> $LOG_FILE

3. Write a script which displays the list of files from the folder given as parameter, sorted by size.

4. Write a script which monitors the modifications made to the /etc/crontab file and sends an e-mail to root if the file is modified. Create a scheduled task to run this script everyday at midnight.

5. Write a script which displays 42.

Clone this wiki locally