Shell Scripting for DevOps Engineers
Table of contents
- What is Shell Scripting for DevOps?
- What is #!/bin/bash? can we write #!/bin/sh as well?
- Write a Shell Script which prints I will become a successful DevOps Engineer
- Write a Shell Script to take user input, input from arguments and print the variables.
- Write an Example of If else in Shell Scripting by comparing 2 numbers.
- Write a bash script create directories.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 Script to backup all your work done till now
- Read About Cron and Crontab, to automate the backup Script
- Read about User Management
- Create 2 users and just display their Usernames
What is Shell Scripting for DevOps?
Shell scripting is a list of commands in a computer program that is run by UNIX shell which is a command line interpreter. Shell scripting helps to automate day-to-day manual tasks and reduce human intervention. This also helps to reduce human errors and time.
Note: Extension of shell script must be ".sh"
Let's look at this basic example of shell script,
Steps:
- Create a new file and open the file,
vi echo.sh
- Enter the following code,
#!/bin/bash
#Author: Madhup Pandey
#Date: 18/07/2023
#Purpose: This script will print some statement on terminal
echo "I will be a great DevOps Engineer"
- Save the file with the following,
Esc + :wq
Here, the "echo" command will print "I will be a great DevOps Engineer" on the terminal.
- Give execute permission to the file,
chmod 777 echo.sh
- execute a shell script with the following command,
./echo.sh
What is #!/bin/bash?
can we write #!/bin/sh
as well?
#!/bin/bash tells the interpreter which shell should be used to execute the shell script.
#!/bin/bash is called a shebang line.
Yes, We can use either,
#!/bin/bash
#!/bin/sh
Both the shebang lines mentioned above are correct.
Note: Use only one of them ( Shebang lines ).
Write a Shell Script which prints I will become a successful DevOps Engineer
#!/bin/sh
#Author: Madhup Pandey
#Date: 18/07/2023
#Purpose: This script will print some statement on terminal
#Printing statement
echo "I will become a successful DevOps Engineer"
Write a Shell Script to take user input, input from arguments and print the variables.
#!/bin/sh
#Author: Madhup Pandey
#Date: 18/07/2023
#Purpose: This shell script will accept 2 Arguments from user and displays it
#Defining Variables
Arg1=$1
Arg2=$2
#Printing Variables
echo "First argument: $Arg1"
echo "Second argument: $Arg2"
Here,
Arg1, --Variable1
Arg2, --Variable2
Execute it with,
./input.sh madhup pandey
Write an Example of If else in Shell Scripting by comparing 2 numbers.
#!/bin/sh
#Author: Madhup Pandey
#Date: 18/07/2023
#Purpose: This shell script will compare 2 numbers using if else
#Defining Variables
Num1=8
Num2=10
#Comaring Variables
if [ $Num1 -lt $Num2 ]
then
echo "$Num1 is greater than $Num2"
elif [ $Num1 -eq $Num2 ]
then
echo "$Num1 is equal to $Num2"
else
echo "$Num1 is lesser than $Num2"
fi
Write a bash script create directories.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.
#!/bin/bash
#############################################################
#Author: Madhup Pandey
#Date: 19/07/2023
#Purpose: This script will create 90 directories at once
#############################################################
#Intializing variables
Name_of_dir=$1
start_no=$2
end_no=$3
#Command to create directories
eval mkdir $Name_of_dir{$start_no..$end_no}
Here, "eval" command takes arguments as input to the command and stores it as one single command.
Execution,
./directories.sh day 1 90
1, --Second argument
create a Script to backup all your work done till now
##########################################################################
#Author: Madhup Pandey
#Date: 19/07/2023
#Purpose: This script will take backup
##########################################################################
#Creating Variables
src=/home/ubuntu/day5
dest=/home/ubuntu/backups
time=$(date +"%Y-%m-%d-%H-%M-%S")
backupfile=$dest/$time.tgz
#Taking Backup
echo "Taking backup on $time"
tar zcvf $backupfile --absolute-names $src
if [ ${?} -eq 0 ]
then
echo "Backup Complete"
else
exit 1
fi
Above, script will take backups.
Read About Cron and Crontab, to automate the backup Script
crontab -e
Enter the below code in the last line of crontab,
* * * * * sh /home/ubuntu/day5/backup.sh
first *, --represents minutes
second *, --represents hours
third *, --represents day of month
fourth *, --represents month
fith *, --day of week
Read about User Management
User management is a very important part of DevOps engineer's journey.
User management includes some basic tasks, like
Deleting Users
Password reset
Create 2 users and just display their Usernames
##########################################################################
#Author: Madhup Pandey
#Date: 19/07/2023
#Purpose: This script will take two argument as username and create user
##########################################################################
#Creating variables
user1=$1
user2=$2
#Creating Users
echo "Adding Users $user1 and $user2"
useradd -p test -m $user1
useradd -p test -m $user2
if [ ${?} -eq 0 ]
then
echo "Users added successfully"
else
exit 1
fi