Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name and second is start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
Do the same using Shell Script i.e using either Loops or command with start day and end day variables using argument
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
dir_name="$1"
start="$2"
end="$3"
if ! [[ "$start" =~ ^[0-9]+$ ]] || ! [[ "$end" =~ ^[0-9]+$ ]]; then
echo "Start and end numbers must be integers."
exit 1
fi
if [ "$start" -gt "$end" ]; then
echo "Start number must be less than or equal to end number."
exit 1
fi
for ((i=start; i<=end; i++)); do
dir="$dir_name$i"
mkdir "$dir"
echo "Created directory: $dir"
done
echo "Directories created successfully."
The line if ! [[ "$start" =~ ^[0-9]+$ ]] || ! [[ "$end" =~ ^[0-9]+$ ]]; then
is used to validate whether the values of start
and end
provided as command-line arguments are valid integers. Let's break down the components of this line:
[[ "$start" =~ ^[0-9]+$ ]]
: This part checks if the value of the variablestart
matches the pattern^[0-9]+$
, which signifies a string containing only digits (0-9). In regular expressions,^
indicates the start of the string,[0-9]
matches any single digit, and+
indicates that the preceding character or group (in this case,[0-9]
) can appear one or more times. So, this part checks if the value ofstart
is a valid non-negative integer.! [[ "$start" =~ ^[0-9]+$ ]]
: The!
symbol is a logical NOT operator. So,! [[ "$start" =~ ^[0-9]+$ ]]
evaluates to true if the value ofstart
is not a valid non-negative integer.||
: This is the logical OR operator. It combines two conditions and returns true if at least one of the conditions is true.! [[ "$end" =~ ^[0-9]+$ ]]
: Similar to the first part, this checks if the value ofend
is not a valid non-negative integer.
Combining these components, the entire line checks if either the start
value or the end
value is not a valid non-negative integer. If either of them is not a valid integer, it means the user didn't provide proper input, and the script should exit with an error message.
In simpler terms, this part of the script ensures that both start
and end
are valid integers before proceeding with creating the directories. If either of them is not a valid integer, the script prints an error message and exits.
Save this script in a file named createDirectories.sh, make it executable using chmod +x createDirectories.sh, and then you can run it as shown in your example:
$ ./createDirectories.sh day 1 90
This will create directories named day1, day2, ..., day90. Just ensure the script has the necessary permissions to create directories in the location where you run it.
The output would look like when you run the script with the provided example:
$ ./createDirectories.sh day 1 3
Created directory: day1
Created directory: day2
Created directory: day3
Directories created successfully.
In this example, the script was executed with the arguments day, 1, and 3, which created directories named day1, day2, and day3 in the current directory. The output messages indicate the creation of each directory and then a final message confirming the successful creation of directories.
Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.
HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning