Shell Scripting for DevOps
Concepts include Shell, Kernel, types of Shells, Shebang, Scripting 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 Default Shell of 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