#Day : 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
Today, We have 3 tasks to perform on day 5.Let's start
Task 1 : 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.
Solution : I have written shell script.
#!/bin/bash
#author : Ajay Patel
#date : 1 may 2023
#version : 1.0
directory_name=$1
start_number=$2
end_number=$3
if [[ -z $directory_name || -z $start_number || -z $end_number ]]; then
echo " please provide directory name , start number , and end number as arguments"
exit 1
fi
if ! [[ $start_number =~ ^[0-9]+$ && $end_number =~ ^[0-9]+$ ]]; then
echo "start number and endnumber must be numeric"
exit 1
fi
if [[ $start_number -gt $end_number ]]; then
echo "start number cannot be greater than end number"
exit 1
fi
for ((number = start_number; number <= end_number; number++)); do
dir_name="${directory_name}_${number}"
mkdir "$dir_name"
echo "Created directory: $dir_name"
done
In this shell script First it will check whether all the variable is provided or not (for example : "name of the folders , start number and end number") then after it will check whether given input is number or not as start number and end number. and last it will check whether start number is less than end number or not. If everything is okay then it will start for loop and will create requested folders.
Output:

Task 2 : Create a Script to backup all your work done till now.
Here is my script :
#!/bin/bash
#author: Ajay Patel
#date: 18-04-2023
echo "Starting back up"
#setting up source and destination folder
src=/home/ubuntu/scripts
tgt=/home/ubuntu/backup
#getting current timestamp
new_name=$(date +%Y%m%d%H%M%S)
#setting up path for back with .extention
backup_path=${tgt}/${new_name}.tar.gz
#creating zip file
tar --absolute-names -czf $backup_path $src
echo "Backup of $src created as $backup_path"
In this script , we have just set src and tgt directory then store current timestamp into new variable. we have used tar library to create zip(.tar.gz).
Last task of the day : Create 2 users and just display their Usernames
sudo useradd A
sudo useradd B
To display all the user :
getent passwd | cut -d ':' -f 1
It was nice challenge. we have learned some condtion and loop in today's scripts. Looking forward to new challenges.



