Day 5: Master Advanced Linux Shell Scripting and User Management for DevOps

Task 1:Create Directories Using Shell Script:

Description: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 if exactly 3 arguments are provided
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Assign arguments to variables for readability
dir_name=$1
start_num=$2
end_num=$3

# Loop to create directories from start_num to end_num
for (( i=start_num; i<=end_num; i++ ))
do
  # Create directory with the format: directory_name followed by the current number
  mkdir "${dir_name}${i}"
  echo "Directory ${dir_name}${i} created"
done

echo "All directories created successfully."

Explanation:

  1. Argument Check: The script first checks if three arguments are passed; otherwise, it shows the correct usage and exits.

  2. Assigning Variables: The input arguments are stored in variables (dir_name, start_num, and end_num).

  3. Loop for Directory Creation: A for loop iterates from the start number to the end number. Inside the loop:

    • Each directory is created using mkdir, with the format directory_name + number.

    • A confirmation message is printed for each directory created.

  4. Final Message: After the loop completes, it displays a success message.

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

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

Explanation:

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

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries to cron. A crontab file is a user file that holds the scheduling information.

  • Cron is a job scheduler utility, In simple terms it executes described commands/script at a particular predefined interval.

  • crontab is basically a table in which we maintain all the cron jobs that we need to execute in our system.

Task 4: Create Two Users

#!/bin/bash

# Define usernames
USER1="user1"
USER2="user2"

# Create the users
sudo useradd -m "$USER1"
sudo useradd -m "$USER2"

# Set passwords for each user (you can specify passwords here if needed)
echo "$USER1:password1" | sudo chpasswd
echo "$USER2:password2" | sudo chpasswd

# Display the usernames
echo "Created users:"
echo "Username: $USER1"
echo "Username: $USER2"

Explanation of Each Command

  • useradd -m <username>: Creates a new user with the specified username and the -m flag to create a home directory.

  • echo <username>:<password> | sudo chpasswd: Sets a password for the user by piping it to the chpasswd command.

  • Display Usernames: The script outputs the usernames after they’re created.