Day 05: Advanced Linux Shell Scripting for DevOps Engineers
In our previous article, “Basic Linux Shell Scripting,” we embarked on a journey to demystify the fundamentals of shell scripting for aspiring DevOps practitioners. Today we will have a hands on commands and will write some advance shell scripts.
Questions
1) You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -
So 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.
Example 1: When the script is executed as
```./createDirectories.sh day 1 90```
then it creates 90 directories as ```day1 day2 day3 …. day90```
Ans=>
#!/bin/bash
$directory_name
$start_number
$end_number
echo “Enter your directory name”
$directory_name
echo “Enter your start number”
$start_number
echo “Enter your end number”
$end_number
# Create the directories
for ((i=start_number; i<=end_number; i++)); do
dir_name=”${directory_name}_${i}”
mkdir “$dir_name”
done
echo “Directories created successfully!”
2) Create a Script to backup all your work done till now.
Backups are an important part of DevOps Engineers day to Day activities
Ans=>
#!/bin/bash
src_dir=/home/ubuntu/scripts
tgt_dir=/home/ubuntu/backup
curr_timestamp=$(date “+%Y-%m-%d-%H-%M-%S”)
backup_file=$tgt_dir/$curr_timestamp.tgz
echo “$backup_file”
echo “taking backup at $curr_timestamp”
tar czf $backup_file $src_file ./*
echo “backup file”
3) Create 2 users and just display their Usernames
Ans=>
#!/bin/bash
add_user()
{
USER=$1
PASS=$2
useradd -m -p $PASS $USER && echo “Successfully added $user”
}
#MAIN
add_user radhey test@123