🚀 Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management

🚀 Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management

¡

3 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.

Example 1: When executed as ./createdirectories.sh day 1 90, it creates 90 directories as day1 day2 day3 ... day90.

#!/bin/bash
# Check if exactly three arguments are passed
if [ $# -ne 3 ]; then
  echo "Usage: Please enter valid arguments $0 day 1 90"
  exit 1
fi
# Assign the arguments to variables
dir_name=$1
start_num=$2
end_num=$3
# Create directories in the specified range using loop
for ((i=start_num; i<=end_num; i++))
do
  mkdir ${dir_name}${i}
done
echo "All directories created successfully."

Example 2: When executed as ./createDirectories.sh Movie 20 50, it creates 31 directories as Movie20 Movie21 Movie22 ... Movie50.

#!/bin/bash
# Check if exactly three arguments are passed
if [ $# -ne 3 ]; then
  echo "Usage: $0 Movie 20 50"
  exit 1
fi
# Assign the arguments to variables
dir_name=$1
start_num=$2
end_num=$3
# Create directories in the specified range using loop
for ((i=start_num; i<=end_num; i++))
do
  mkdir ${dir_name}${i}
done
echo "All directories created successfully."

Task 2: Create a Script to Backup All Your Work:

Backups are an important part of a DevOps Engineer's day-to-day activities.

#!/bin/bash
backup_source="home/ubuntu" # Source directory to backup
backup_dir="/home/ubuntu/Backup" # Destination where the backup will be stored
timestamp=$(date +"%Y%m%d_%H:%M:%S") # Generate time stamp
backup_file="${backup_dir}/backup_${timestamp}.zip" # Backup file name with timestamp

# Create Backup in Zip folder
zip -r $backup_file "$backup_source"

echo "Backup successfully created at $backup_file"

Task 3: Read About Cron and Crontab to Automate the Backup Script:

Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule scripts or commands to run automatically at specified intervals (e.g., daily, weekly, or monthly). The crontab (cron table) is the file that defines the scheduled tasks for a specific user.

Key Concepts of Cron and Crontab:

  1. Cron Daemon:

    • The cron daemon (crond) runs in the background and checks the crontab files every minute to see if any scheduled jobs need to be executed.
  2. Crontab File:

    • Each user can have their own crontab file. You can view or edit your crontab using the crontab command.
  3. Crontab Format:

    command_to_execute

* * * * *
| | | | |

| | | | +---- Day of the week (0 - 7) (Sunday is both 0 and 7)

| | | +------ Month (1 - 12)

| | +-------- Day of the month (1 - 31)

| +---------- Hour (0 - 23)

+------------ Minute (0 - 59)

The above schedules set in Cron will run the backup.sh script in every 2 hours.

Task 4: Read About User Management:

A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.

Create 2 users and display their usernames.

#!/bin/bash
# Get Usernames from User
read -p "Enter the first username:" user1
read -p "Enter the Second username:" user2
# Create Users
echo "Creating Users"
sudo useradd -m "$user1"
sudo useradd -m "$user2"
echo "Users created successfully"
# Set passwords for created users
echo "Setting password for $user1"
sudo passwd "$user1"
echo "Password successfullly set for $user1"
echo "Setting password for $user2"
sudo passwd "$user2"
echo "Password successfully set for $user2"

Â