Shell Scripting for DevOps
Concepts include Shell, Kernel, types of Shells, Shebang, Scripting, Elements and more..
Linux Architecture
User <> Shell <> Kernel <> Hardware
SHELL:
Shell is a mediator between User and Kernel.
Shell is Responsible for Taking Commands(instructions) from the User and verifying command syntax whether it is valid or not.
Shell shows an error message for invalid commands.
After command validation, Shell gives the instructions to Kernel to Process the Commands given by the User.
A shell is an environment in which we can run our commands, programs, and shell scripts.
When the user logs in, OS starts a shell for the user.
KERNEL:
The kernel is the Mediator between Shell and Hardware.
Kernel executes the commands with System Hardware Components.
Kernel interacts with hardware to perform operations like create, delete, copy, rename etc.
Using kernel only the user can access utilities provided by the operating system.
SCRIPTING:
Set of commands in a file for execution.
Scripting is used to Automate routine tasks. (manual tasks)
Executing a set of commands using a script file (contains a set of commands) is called Shell Scripting.
Shell reads the script file after validation it gives the instructions to Kernel.
The kernel gives the Instructions to Hardware Components to perform the tasks(given in the script file)
{{Types of Shells}}
When the user logs in, OS starts a shell for the user.
1.The C Shell:
It incorporated features such as command history.
It includes helpful programming features like built-in arithmetic and C-like expression syntax.
Denoted as csh
Command full-path name is /bin/csh
Non-root user default prompt is hostname %
Root user default prompt is hostname #
2.The Bourne Shell:
It is the original UNIX shell. It is the default shell for Solaris OS.
It is faster and more preferred.
It lacks interactive features like the ability to recall previous commands.
It also lacks built-in arithmetic and logical expression handling.
Denoted as sh
Command full-path name is /bin/sh and /sbin/sh
Non-root user default prompt is $
Root user default prompt is #
3.The Korn Shell:
It is faster than the C shell.
It is a superset of the Bourne shell. So it supports everything in the Bourne shell.
It has interactive features, built-in arithmetic and C-like arrays, functions, and string-manipulation facilities.
It is denoted as ksh
Command full-path name is /bin/ksh
Non-root user default prompt is $
Root user default prompt is #
4.GNU Bourne-Again SHell(BASH):
Most people use this shell.
It includes features from Korn and Bourne shells.
Denoted as bash
Command full-path name is /bin/bash
Default prompt for a non-root user is bash-<v.mv>$
Root user default prompt is bash-<v.mv>#
Note:v.mv indicates MajorVersion.MinorVersion
Example: bash-3.50$
5.T Shell:
Used operating systems like Linux, FreeBSD, and macOS.
Denoted as tsh
Command full-path name is /bin/tcsh
Default prompt for a non-root user is <username>:~>
Root user default prompt is root@<username>:~#
6.Z Shell:
Z Shell is an extended version of the Bourne-Again Shell (bash), with additional features and capabilities.
Denoted by zsh
Command full-path name is /bin/zsh
Default prompt for a non-root user is <username>(user):~%
Root user default prompt is root@<username>:~#
Checking available shells:
cat /etc/shells
Check the Default Shell of the Linux machine:
echo $SHELL
{{SHELL SCRIPING}}
Choose Shell type to process our file execution using SHE-BANG(#!)
Syntax:
<Shebang> <Shell-path>
Example:
#! /bin/bash
Creating Script file:
shell script files have the extenstion .sh
Syntax:
<filename>.sh
Creating file:
vi <filename>.sh
press i (Insert Mode): to insert or edit the file
Writing Script:
#! <Selected Shell-path>
<commands/script>
Press Esc key and :wq (to Write(Save) and Quit)
Press Esc key and :q! (Quit without Saving)
Example:
#! /bin/bash
echo " welcome"
echo " to"
echo " shell script"
cal
date
whoami
Press Esc key and :wq (to Write(Save) and Quit)
Executing script file:
Before executing check whether script files have execute permissions or not.
giving User eXecute permissions to script file:
sudo chmod 700 <filename.sh>
or
sudo chmod u+x <filename.sh>
Running script file:
sh <filename.sh> (directly executes)
or
./<filename.sh> (checks execute permissions and then runs)
{{SAMPLE SCRIPTS}}
Note: Here I'm using BASH Shell in the Script file.
Read data from the user and display the result:
#! /bin/bash echo " Enter your name" read name echo " Hi $name Have a Good Day" echo " Welcome to shell script"
Result:
If the name is : Dev Result will be like: Hi Dev Have a Good Day Welcome to shell script
Read 2 Values from the user and show the result of arithmetic operations:
#! /bin/bash echo "Enter a" read a echo "Enter b" read b c=$(($a + $b)) echo " Sum of $a and $b is $c"
Result:
If the values of a=10 and b=20 then, Result will be like: Sum of 10 and 20 is 30
{{COMMENTS}}
single line comments:
Syntax:
# <content>
Example:
# main script starts here
multiple line comments:
Syntax:
<<COMMENT
<content>
<content>
<content>
COMMENT
Example:
<<COMMENT
Shell is a mediator between User and Kernel.
Shell is Responsible for Taking Commands(instructions) from the User
and verifying command syntax whether it is valid or not.
Shell shows an error message for invalid commands.
COMMENT
{{SLEEP}}
used to "hold/stop/pause" the file execution for some time.
syntax:
sleep <value>
Example:
sleep 30s
{{COMMAND LINE ARGUMENTS}}
Arguments are given at the time of execution.
Used to supply values dynamically to the script file.
Syntax:
sh <filename.sh> <1st Argument> <2nd Argument>...etc
Example:
sh abc.sh Dev 30
Declaring "command line arguments" in Script files by #, numbers(1,2,3....) and * along with $:
$# - to display the Number of Arguments
$0 - to display Script filename
$1 - to display first command line argument
$2 - to display second command line argument
$3 - to display third command line argument .......
$* - to display all command line arguments
Sample for Declaring "command line arguments" in Script files :
Sample Script File:
#! /bin/bash
echo $#
echo $0
echo $1
echo $2
echo $3
echo $4
echo $*
Execution:
syntax:
sh <filename.sh> <command line argument> <command line argument> <command line argument> etc...
Example:
sh abc.sh dev 30 devops india
Result of example:
4
dev
30
devops
india
dev 30 devops india
Explination:
4 for echo $#(displays no of command line arguments)
[command line arguments are 1st=dev , 2nd=30 , 3rd=devops 4th=india) ] abc.sh for echo $0(displays filename)
dev for echo $1(displays first command line argument)
30 for echo $2(displays second command line argument)
devops for echo $3(displays third command line argument)
india for echo $4(displays forth command line argument)
dev 30 devops india for echo $*(displays all command line argument)
{{Key elements of Shell Scripting}}
Variables
Conditional statements
Loops
Functions
Switch-case Statements
1. {{Variables in Shell Scripting}}
Variables are used to store data. variables are key-value pairs
to check variable value: echo $<VARIABLE NAME> Example: echo $SHELL echo $USER echo $NAME
The declared variables (like NAME, AGE etc) are accessed by using $ (like $NAME,$AGE etc)
Declare a variable name: #! /bin/bash NAME=Dev NAME=Vasu echo $NAME Note: Here first it will take "Dev" as value for Variable "NAME" and then replace the value as "Vasu" for variable "NAME".
๐กwe can also give other values to NAME Variable.Declare a readonly variable: (we can restrict to change the value of a variable using readonly) #! /bin/bash readonly NAME=Dev NAME=Vasu echo $NAME Note: Here, we used readonly to restrict variable modifications. the first value for "NAME" variable is "Dev" and it is readonly, So "NAME" does't accept the later values.
๐กwe can not modify the value of "NAME" when using readonly.In Shell Scripting there is No concept of Datatypes ( like Int, string, float...) everything is treated as text data or string data.
Variable rules:
we should not use special symbols in variables (like @#$% etc)
The variable name should start with the alphabet only and the variable should not start with a Number/digit (like 1a,2c etc).
It is recommended to use UPPERCASE characters.
Types of variables:
Pre-defined or Environmental Variables or system variables:
These variables are already defined and used by our System or OS. Examples of Environmental variables: HOME โ The user's home directory location. SHELL โ Current shell (bash, zsh, etc.). LOGNAME โ Name of the user. UID โ User's unique identifier .....etc., check variables by: echo $SHELL echo $USER echo $HOME
User-defined variables:
based on user requirement, variables are defined by the User are used-defined variables ( like name, age, city, etc.,)
2. {{Conditional Statements}}
Conditional statements (IF, ELIF) are used to execute commands, based on a given condition.
Single Condition (IF):
syntax:
if <condition>
then
<execute this>
else
<execute this>
fi
Example Script for Single Condition:
#! /bin/bash
echo "enter name"
read name
echo "enter marks"
read marks
if [ $marks -ge 50 ]
then
echo "$name, you are eligible"
else
echo "$name, you are not eligible"
fi
Code image in the Script file:
Image of Execution:
Multiple Conditions(ELIF):
syntax:
if <condition>
then
<execute this>
elif <condition>
then
<execute this>
elif <condition>
then
<execute this>
.
.
.
.
else
<execute this>
fi
Example Script for Multiple Conditions:
#! /bin/bash
echo "enter name"
read name
if [ $name == "abhi" ]
then
echo "$name , number is 1"
elif [ $name == "khyati" ]
then
echo "$name , number is 2"
elif [ $name == "dev" ]
then
echo "$name , number is 3"
else
echo "$name , number not assigned to you"
fi
Code image in the Script file:
Image of Execution:
3. {{LOOPS}}
Types of Loops:
Range-based loops (FOR):
We use these loops, If we want to execute the loop a certain number of times.
In a range-based loop like FOR loop, first, it will check the "range".
Format: for ( (<Initialise> ; <Condition> ; <Increment/Decrement> ) ) do echo <execute this> done Initialise : no data types in shell scripting Condition : Condition Range for Execution Increment/decrement : Increment/decrement for each execution
Syntax: for ((i=1; i<=<n>; i++)) do echo "$i" done
Example Script for FOR Loop:
#! /bin/bash echo "enter n:" read n for ((i=1; i<=n; i++)) do echo "$i" done
Code image in the Script file:
Image of Execution:
Conditional-based loops(WHILE):
We use these loops to Execute the loop, till the given condition satisfies.
The Conditional-based loops like WHILE Loop are dependent on given "Condition"
Syntax:
while <conditon>
do
echo <execute this>
let <increment/decrement>;
done
Example Script for WHILE Loop:
#! /bin/bash
echo "enter n:"
read n
echo "The Reverse order of $n Numbers is:"
i=$n
while [ $i -ge 0 ]
do
echo "$i"
let --i;
done
Code image in the Script file:
Image of Execution:
INFINITELoops:
When the Condition is always satisfied, Loop executes forever.
Example:
while true
do
echo "Loop statement"
done
Note: when the loop execution is infinite, press Ctrl+C
to stop infinite loop.
Script:
#! /bin/bash
while true
do
echo "This is the example of infinite loop"
done
Image of Execution:
4. {{FUNCTIONS}}
FUNCTION is a logical unit to perform some action.
Instead of doing huge tasks, logically divide the huge task into small tasks using functions.
the function is used to perform an action.
functions are reusable.
Syntax:
#Creating fuction:
function <FunctionName>() {
<Content for execution>
}
#Calling fuction:
<FunctionName>
Example of Function:
#! /bin/bash
function funone( ) {
echo "This is the sample function"
echo "Today is:"
date
echo "***************************"
echo "User is :"
echo $USER
}
funone
Code image in the Script file:
Image of Execution:
Example of Function with Parameters:
#! /bin/bash
function funone ( ) {
echo "$2";
}
funone Devops gitops
funone AWS cloud
funone Azure cloud
funone GCP cloud
Code images in the Script file (3 samples):
Image of Execution (3 samples outputs):
Soon I'll add further content.....till then,
Keep Practicing and Keep Smiling.
Thank you
Yours Loving Dev
#Dev1289