Advanced Linux Shell Scripting for DevOps Engineers with User management
Table of contents
- Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
- Creating a backup script
Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
Create a file 'createdirectorie.sh' using VIM command:
#!/bin/bash
# Check if all arguments are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Assign arguments to variables
directory_name=$1
start_number=$2
end_number=$3
# Create directories
for ((i=start_number; i<=end_number; i++));
do
directory="${directory_name}${i}"
mkdir "$directory"
done
echo "Directories created successfully."
Give the execute permission to the shell file:
./createDirectories.sh Day 1 90
Instead of this run the following command:
$ mkdir newfolder_{1..90}
Creating a backup script
#!/bin/bash
# Checking for the number of arguments passed is correct
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Extracting arguments to variables
dir_name="$1"
start="$2"
end="$3"
# Create directories with dynamic names
for ((i="$start"; i<="$end"; i++)); do
dir="$dir_name$i"
mkdir -p "$dir"
done
echo "Directories are created successfully"