Day 4 - Basic Linux Shell Scripting for DevOps Engineer

Day: 4 - Introduction to Linux Shell Scripting
Shell is an interface between the user and the kernel. Even though there can be only one kernel; a system can have many shell running simultaneously. So, whenever a user enters a command through the keyboard, the shell communicates with the kernel to execute it and then displays the output to the user.
Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.
A kernel is considered the main component of Linux OS. It is simply a resource manager that acts as a bridge between hardware and software. Its main role is to manage hardware resources for users and is generally used to provide an interface for user-level interaction. A kernel is the first program that is loaded whenever a computer system starts. It is also referred to as low-level system software.
The main functions of Kernel:
Memory Management
Process Management
Device Management
Storage Management
#!/bin/bash is the first line of the script and is called Shebang. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.
There are different shebang lines for each different shell.
#!/bin/bash --> the script should always be run with bash, rather than another shell.
#!/bin/sh --> the interpreter should be a Bourne shell, not Bash.
#!/bin/csh --> interpreter here is a C shell.
#!/bin/ksh --> interpreter is Korn shell.
So we can use any shell type in our script as per the use case and availability of the type of shell in our system.
Swap space, as the name suggests, is a space on a hard disk that is used when the amount of physical memory or RAM is full. It is considered a substitute for physical memory. Its main function is to substitute disk space for RAM when real RAM does not have enough space to hold all programs that are executing, and more space is required. In simple words, it can be used as an extension of RAM by Linux.
- Write a Shell Script that prints “I will complete #90DaysofDeVops challenge"
/scripts$ vim scripts.sh
#!/bin/bash
echo "I will complete the 90Daysof DevOps challenge"
/scripts$ chmod 777 scripts.sh
/scripts$ ls -l
/scripts$ ./scripts.sh
I will complete the 90Daysof DevOps challenge
- Write a Shell Script to Print the Addition of two numbers
$ vim add.sh
#!/bin/bash
#Add Two numeric value
((sum=25+35))
#Print the result
echo $sum
$ ./add.sh
60
- 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 the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
$ nano createDirectories.sh
#!/bin/bash
dir_name=$1
start=$2
end=$3
for (( i=start; i<=end; i++))
do
mkdir "$dir_name$i"
done
$ chmod 777 createDirectories.sh
$ ./createDirectories.sh dir 1 90
$ ls
0 dir18 dir29 dir4 dir50 dir61 dir72 dir83
add.sh dir19 dir3 dir40 dir51 dir62 dir73 dir84
createDirectories.sh dir2 dir30 dir41 dir52 dir63 dir74 dir85
dir1 dir20 dir31 dir42 dir53 dir64 dir75 dir86
dir10 dir21 dir32 dir43 dir54 dir65 dir76 dir87
dir11 dir22 dir33 dir44 dir55 dir66 dir77 dir88
dir12 dir23 dir34 dir45 dir56 dir67 dir78 dir89
dir13 dir24 dir35 dir46 dir57 dir68 dir79 dir9
dir14 dir25 dir36 dir47 dir58 dir69 dir8 dir90
dir15 dir26 dir37 dir48 dir59 dir7 dir80 scripts.sh
dir16 dir27 dir38 dir49 dir6 dir70 dir81
dir17 dir28 dir39 dir5 dir60 dir71 dir82
- Write an Example of If else in Shell Scripting by comparing two numbers
$ vim compare.sh
#!/bin/bash
echo "enter the first number"
read a
echo "enter the second number"
read b
if [ $a = $b ]
then
echo "a is greater then b"
else
echo "b is greater then a"
fi
$ ./compare.sh
Output :-
enter the first number
15
enter the second number
22
b is greater then a
- Write an Example of do while loop in shell scripting to print the numbers
$ vi print.sh
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
$ chmod 777 print.sh
$ ./print.sh
Output :-
1
2
3
4
5
- Create a Script to back up all your work done till now.
$ vi backup.sh
#!/bin/bash
src_dir=/home/user/Day5
tgt_dir=/home/user/backups
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking backup on $curr_timestamp"
tar -cvzf $backup_file --absolute-names $src_dir
echo "Backup complete"
$ chmod 777 backup.sh
$ ./backup.sh
Thank you for reading! Happy Learning!!
Pooja
About the author
Hey!! I am a Pooja Patil. I started writing articles on DevOps and cloud journey. My purpose is share the concept knowledge that i learn. the task that i perform regarding Devops. Welcome to my Blog!!!
Comments
Join the discussionNo comments yet. Be the first to comment.

