Day 5: Advanced Linux Shell Scripting & User Management for DevOps Engineers

Manav RautManav Raut
4 min read

Task 1: Create Directories Using Shell Script:

- Write a bash script createDirectories.sh that, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.

#!/bin/bash

# Check for the correct number of arguments
if [[ $# -ne 3 ]]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Get script arguments
dir_name="$1"
start_num="$2"
end_num="$3"

# Loop through numbers and create directories
for (( i=$start_num; i<=$end_num; i++ )); do
  # Create directory with dynamic name
  mkdir "$dir_name$i"
  if [[ $? -ne 0 ]]; then
    echo "Error creating directory: $dir_name$i"
  fi
done

echo "Created directories from $dir_name$start_num to $dir_name$end_num"

Explanation:

  1. Shebang (#!): The first line #!/bin/bash specifies the interpreter for the script, which is bash in this case.

  2. Argument Check:

    • if [[ $# -ne 3 ]]: This checks if the number of arguments passed to the script is not equal to 3 ($# represents the number of arguments).

    • echo "Usage...": If not 3 arguments, it displays the correct usage message.

    • exit 1: Exits the script with an error code (1).

  3. Get Arguments:

    • dir_name="$1": Stores the first argument (directory name) in dir_name.

    • start_num="$2": Stores the second argument (start number) in start_num.

    • end_num="$3": Stores the third argument (end number) in end_num.

  4. Loop and Create Directories:

    • for (( i=$start_num; i<=$end_num; i++ )): This for loop iterates from start_num to end_num with increment of 1 in variable i.

    • mkdir "$dir_name$i": Inside the loop, it creates a directory with the name formed by concatenating dir_name and the current value of i.

    • if [[ $? -ne 0 ]]: This checks if the mkdir command returned an error (non-zero exit code).

      • echo "Error creating directory: $dir_name$i": If an error occurs, it displays an error message with the specific directory name.
  5. Success Message:

    • After the loop completes, it prints a success message indicating the range of created directories.

Task 2: Create a Script to Back Up All Your Work:

- Backups are an important part of a DevOps Engineer's day-to-day activities. The video in the references will help you understand how a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, nothing is impossible).

#!/bin/bash

# Set your project directory and backup destination

SOURCE_DIR="$HOME/my_project"  # Change this to your project directory
BACKUP_DIR="$HOME/backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="${BACKUP_DIR}/backup_${TIMESTAMP}.tar.gz"

# Create the backup directory if it doesn’t exist

mkdir -p $BACKUP_DIR

# Compress and back up the source directory

tar -czf $BACKUP_FILE $SOURCE_DIR

# Check if the backup was successful

if [ $? -eq 0 ]; then
    echo "Backup successful! File stored at: $BACKUP_FILE"
else
    echo "Backup failed!"
fi

How it Works:

  • It compresses your project folder and creates a backup in the backups folder.

  • The backup filename includes a timestamp for easy tracking (e.g., backup_20231011_143000.tar.gz).

  • If the backup succeeds, you’ll see a success message. ✅

You can now run this script manually whenever you need a backup!


Task 3: Automate the Backup with Cron ⏰

Let’s automate the backup process using cron—a Linux tool for scheduling tasks. Instead of running the backup script manually, we’ll schedule it to run automatically every day at midnight. 🌙

Steps:

  1. Open the cron job editor:
crontab -e
  1. Add this line to schedule the backup:

     0 0 * /path/to/backup.sh
    

    This tells the system to run backup.sh every day at 12:00 AM.


Task 4: Create Two Users

Linux allows you to manage users on your system easily. Let’s create two new users using the useradd command.

Steps:

  1. Create two users:

     sudo useradd -m user1
     sudo useradd -m user2
    
  2. Set passwords for the users:

     sudo passwd user1
     sudo passwd user2
    
  3. Display the usernames:

     awk -F':' '{ print $1 }' /etc/passwd | tail -n 2
    

The above command will show the last two users created on the system, which are user1 and user2.


0
Subscribe to my newsletter

Read articles from Manav Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manav Raut
Manav Raut