Day 05: Automating Directory Creation with Bash Scripting
Mastering Automation - Create Dynamic Directories with Bash Scripting
Table of contents
Introduction🎉
Hello, fellow DevOps learners! 👋
It's Day 05 of the 90 Days of DevOps Challenge, and we're diving into the wonderful world of bash scripting to automate directory creation. Are you ready to demystify this concept and level up your DevOps skills? Let's get started! 🚀
Today, we'll explore how to create a bash script that generates multiple directories with dynamic names. This automation will save you time and effort when setting up your projects.
Concepts:
Bash scripting fundamentals
Command-line arguments and variables
Loops and dynamic directory naming
Script execution and error handling
Tasks:
Task 1: Setting up the bash script
Let's start by setting up our script file. Open your favorite text editor, like VSCode or Nano, and create a new file named create_directories.sh
.
Task 2: Define the bash script header
Begin the script by indicating that it should be executed using bash. Don't forget to add comments to explain the script's purpose and functionality.
#!/bin/bash
# Script: create_directories.sh
# Author: Your Name
# Date: Today's Date
# Purpose: Generate directories with dynamic names
Task 3: Reading the input arguments
We'll read three arguments from the command line: directory name, start number, and end number. These will help us create a specific number of directories.
#!/bin/bash
# Script: create_directories.sh
# Author: Your Name
# Date: Today's Date
# Purpose: Generate directories with dynamic names
# Read command-line arguments
dir_name=$1
start_num=$2
end_num=$3
Task 4: Implementing the directory creation loop
Let's use a loop to create the directories. The loop will iterate from the start number to the end number, generating dynamic directory names.
#!/bin/bash
# Script: create_directories.sh
# Author: Your Name
# Date: Today's Date
# Purpose: Generate directories with dynamic names
# Read command-line arguments
dir_name=$1
start_num=$2
end_num=$3
# Loop for directory creation
for ((i=start_num; i<=end_num; i++))
do
dir="$dir_name$i"
mkdir "$dir"
done
Task 5: Adding error handling and validation (optional)
You can add error handling to ensure the correct usage of the script. Check if the correct number of arguments is provided and if the numbers are valid.
Task 6: Making the script executable
Before running the script, make sure it has the necessary permissions to execute.
#!/bin/bash
# Script: create_directories.sh
# Author: Your Name
# Date: Today's Date
# Purpose: Generate directories with dynamic names
# Read command-line arguments
dir_name=$1
start_num=$2
end_num=$3
# Loop for directory creation
for ((i=start_num; i<=end_num; i++))
do
dir="$dir_name$i"
mkdir "$dir"
done
Task 7: Test the script
Run your script with different input combinations to ensure it generates directories as expected.
./create_directories.sh dir_ 1 5
Conclusion✨
That's it for Day 05! You've successfully automated directory creation using a bash script. 🎉 Stay tuned for Day 06, where we'll explore more exciting DevOps concepts!
Remember, practice makes perfect! Happy scripting! 😊👍