Lab 5: Bash scripting

Exercise 1 - Bash basics

Task 1 - Bash scripting basics

Task 2 - Bash loops and conditions

Task 3 - If statements

Task 4 - Bash functions

Exercise 2 - Working with files and directories

Task 1 - File and directory test operators

There are several options in bash to check the type of file you are interacting with. In many cases, the options are also used to check for the existence of a specified file or directory. The example below shows the options that can be used.

Task 2 - Directory and file manipulation

Task 3 - Jump directories

Sometimes it is difficult to navigate directories with the possibly infinite number of parent directories we need to provide. For example cd ../../../../../. Let’s create a script that will help us jump to a specified directory without executing cd ../.

Exercise 3 - Hash tables and more bash usage

Task 1 - Hash tables in bash

A dictionary, or a hashmap, or an associative array is a data structure used to store a collection of things. A dictionary consists of a collection of key-value pairs. Each key is mapped to its associated value.

Task 2 - Use sed in bash

Task 3 - Execute Python commands in bash

Exercise 4 - Debugging bash scripts

Task 1 - Command exit code

You can verify whether a bash command executed successfully by viewing the exit status code of the command. The exit status of the previously executed command is stored in the $? variable. A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.

Task 2 - Using set -xe

When there is an error that stops the execution of a bash script, the bash interpreter usually displays the line number that triggered the error. However, in some cases, it might be necessary to trace the flow of execution of the script. This provides more insight into the conditions that are met, and the state of the loops.

Questions to answer

  1. Write a bash script that displays the following details of the logged-in user from the environment variables:

    • Login username
    • Home directory
    • Shell
    • The hostname of the system
    • The script should extract the IP address of the system from the ifconfig or ip command. Save the IP address to the ipaddress variable and display it as output.

    Sample output:

    Username: user1
    Home Directory: /home/user1
    Shell: /bin/bash
    Hostname: ubuntuvm
    IP address: 10.1.1.1
    
  2. Backups are important in system administration. Create a script that will backup your home directory.

    • The backup file should be compressed to tar.gz.
    • All files and directory permissions should be preserved in the backup.
    • The backup destination directory is /var/backups/
    • The script should create the destination directory if it doesn’t already exist.
    • The backup file name should take the format home_backup_month_day_year_hour_minute_second.tar.gz.
      For example home_backup_Feb_18_2023_02_30_02.tar.gz

    A typical real life scenario is keeping backups of websites. Administrators are usually interested in backing up /var/www/html.

  3. Write a bash script that checks various artifacts on the system. The script mainly checks for system information, and OS components. Your script should do the following:

    • Print the OS kernel name and kernel version.
    • Print the system architecture.
    • Print all currently logged in users (show the date or time which the users logged in, and show the command line of the users’ current process).
    • Verify that EFI is enabled and print the relevant output.
    • List all connected block devices (Bonus: Identify the devices that have the GPT partition by adding an * to them in the output).
    • List the first boot device on your system. This should be done according to the boot order in the NVRAM.

    Ensure that the output of your script is neatly formatted and easy to read.
    Bonus points if you create and use at least three functions.

Bonus

  1. Write a bash script that scans the entire system for files that contain the string “/bin/bash”. The script should print only the matches that the currently logged in user has execute permission on.