[ Day-5 ]: Advanced Linux Shell Scripting for DevOps Engineers with User management

[ Day-5 ]: Advanced Linux Shell Scripting for DevOps Engineers with User management

ยท

4 min read

๐ŸŒŸ Introduction ๐ŸŒŸ

Welcome to Day 5 of the #90DaysOfDevOps challenge! Today, we'll dive deep into advanced Linux shell scripting, where we'll focus on essential topics like Creating Dynamic Directories, Automated Backup scripts, Automating the Backup Script with Cron, User Management in Linux, and Creating and Displaying Usernames. As DevOps engineers, mastering shell scripting is vital as it empowers us to automate tasks and optimize processes. ๐Ÿ’ป๐Ÿš€

๐Ÿ“ Creating Dynamic Directories

๐Ÿค“ Deep Dive: Understanding Bash Brace Expansion

Bash supports a feature called 'brace expansion' which is utilized in the above mkdir command. Brace expansion allows you to generate arbitrary strings, especially useful for creating a sequence of numbers or letters.

Here's a breakdown of the command:

  • mkdir: This is the command to create directories in Linux.

  • day{1..N}: The curly braces {} signify the brace expansion. The range 1..N specifies the sequence. When combined with the prefix day, bash interprets this as a sequence of directory names: day1, day2, ... , dayN.

Some other examples of brace expansion:

  • echo {A..Z}: This would output the sequence of capital letters from A to Z.

  • touch file{1..5}.txt: This creates five files: file1.txt, file2.txt, ... , file5.txt.

๐Ÿ”ฅ Pro Tip:

If you're dealing with a sequence that might have leading zeros, bash can accommodate this too. For example:

mkdir day{001..100}

This command will create directories with names day001, day002, ... , day100, ensuring a consistent naming pattern with three digits.

In conclusion, brace expansion is a powerful tool in your shell scripting toolkit. It simplifies tasks that would otherwise require loops or more verbose commands. By mastering this and other features of the Linux shell, you can greatly enhance your efficiency and automation capabilities as a DevOps engineer.

๐Ÿ“ Creating Dynamic using Shell Scripting Directories

Let's create a user-friendly shell script named task1.sh that generates directories with dynamic names.

#!/bin/bash

# Check if three arguments are provided
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi

# Extract arguments
directory_name="$1"
start_number="$2"
end_number="$3"

# Check if start_number and end_number are integers
if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then
  echo "Error: Start and end numbers must be integers."
  exit 1
fi # we can also skip this part but it is good practice to do so

# Loop to create directories
for ((i=start_number; i<=end_number; i++)); do
  directory="${directory_name}${i}"
  mkdir "$directory"
done

echo "Directories created successfully."

This is a straightforward bash script that automates the creation of a sequence of directories based on the user input. Let's break down its functionality:

  1. Check Argument Count: The script first checks if exactly three arguments have been provided. These arguments correspond to the base name of the directories, the starting number, and the ending number, respectively.
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <directory_name> <start_number> <end_number>"
  exit 1
fi
  1. Extract Arguments: The provided arguments are assigned to meaningful variable names to make the rest of the script more readable.
directory_name="$1"
start_number="$2"
end_number="$3"
  1. Check If Arguments Are Integers: The script ensures that the start_number and end_number are integers. This is to avoid potential errors during the directory creation phase.
if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then
  echo "Error: Start and end numbers must be integers."
  exit 1
fi
  1. Directory Creation Loop: A for loop is used to iterate from the start_number to the end_number. For each iteration, the script concatenates the base directory_name with the current iteration number and then creates the directory.
for ((i=start_number; i<=end_number; i++)); do
  directory="${directory_name}${i}"
  mkdir "$directory"
done
  1. Completion Message: Once all directories are created, a success message is displayed.
echo "Directories created successfully."

To use this script, save it to a file, for instance, create_dirs.sh, and then make it executable using chmod +x create_dirs.sh. Now, you can run the script by providing it with the directory base name, starting number, and ending number:

./create_dirs.sh day 1 5

This would create directories named day1, day2, ..., day5.

ย