Skip to content

Scripting

VBrazhnik edited this page Jun 29, 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).

Alternative answer

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

cat /etc/passwd | cut -s -d':' -f1,3,6

Explanation

man cut:

cut - remove sections from each line of files.

Print selected parts of lines from each FILE to standard output.

-d, --delimiter=DELIM — Use DELIM instead of TAB for field delimiter.

-f, --fields=LIST — Select only these fields; also print any line that contains no delimiter character, unless the -s option is specified.

-s, --only-delimited — Do not print lines not containing delimiters.

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.

Answer

#!/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

LOG_FILE=/var/log/update_script.log

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

# To schedule this task add entry '0 4 * * 1 /root/update.sh' in crontab file.
# To do it you need to execute command crontab -e

Useful link

crontab.guru

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

Answer

#!/bin/bash
# List of files, sorted by size

if [ -d $1 ]
then
    ls -lS $1
else
    echo "Path doesn't point to the existing directory"
fi

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.

Answer

#!/bin/bash
# Check /etc/crontab file
# The path to this file must be /root/check.sh

if [ -e /root/hash.md5 ]
then
    if ! md5sum -c /root/hash.md5
    then
	echo "Hash sum didn't match with previous value" | mail -s "Didn't match" root
	md5sum /etc/crontab > /root/hash.md5
    fi
else
    md5sum /etc/crontab > /root/hash.md5
fi

# To schedule this task add entry '@midnight /root/check.sh' in crontab file
# Schedule this task for root user

Explanation

To install the mail command:

apt-get install mailutils

Linux mail command examples – send mails from command line:

To send mail to a local system user just use the username in place of the recipient address

$ mail -s "Hello World" username

You could also append "@hostname" to the username, where the hostname should be the hostname of the current system.

If the email message is in a file then we can use it directly to send the mail. This is useful when calling the mail command from shell scripts or other programs written in perl or php for example.

$ mail -s "Hello World" user@yourmaildomain.com < /home/user/mailcontent.txt

Or a quick one liner

$ echo "This is the message body" | mail -s "This is the subject" mail@example.com

man md5sum:

Print or check MD5 (128-bit) checksums.

-c, --check — Read MD5 sums from the FILEs and check them.

5. Write a script which displays 42.

Answer

#!/bin/bash
# Write a script which displays 42

echo -n '42'