π€What is Shell Scripting?
First a fall, Shell is a command line interface used to communicate with the operating system. Shell scripting in Linux is the process of automating day-to-day or regular tasks on a Linux computer or elsewhere to improve efficiency.
πΌ Purpose of Shell Scripting in DevOps:
-> The tasks that Shell Script performs in DevOps are:
ππ₯οΈ Monitoring Node Health: Checking the performance and status of a node (a computer or device) using the βTOPβ command or custom scripts.
ππ¨βπ» Code Management: Managing the source code of a software project using tools like Git or SVN.
βοΈπ§ Configuration Management: Managing the configuration of a software system using tools like Ansible or Puppet.
πποΈ Infra Management: Managing the infrastructure of a software system using tools like AWS or Azure.
π What is#!/bin/bash
? can we write#!/bin/sh
as well?
The
#!/bin/bash
or#!/bin/sh
is the shebang line at the beginning of a script, indicating the interpreter to use. previously,#!/bin/bash
and#!/bin/sh
are the same, if back to the history Linux redirects#!/bin/sh
to#!/bin/bash
using linking concept. even though the request is taken by "sh
" but forwarded to "bash
".While
bash
andsh
are slightly difference in terms of syntax. for example, way of writing "for Loops" inbash
is totally different to way of writing insh
. However, usingbash
allows access to more features.
Source: https://itslinuxfoss.com
π Examples of Shell Scripting:
1) Shell Script which prints "I will complete #90DaysofDevOps challenge"
?
Syntax:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
2) Shell Script to take user input, input from arguments, and print the variables.
Syntax:
#!/bin/bash
echo "Enter your name:"
read username
echo "Hello, $username! You entered: $1 from arguments."
3) Shell Scripting comparing 2 numbers using the "if-else" condition:
Syntax:
#!/bin/bash
echo "Enter number 1"
read num1
echo "Enter number 2"
read num2
if [ "$num1" -gt "$num2" ]; then
echo "$num1 is greater than $num2"
else
echo "$num1 is lesser than $num2"
fi
Notes:
Use
"$num1"
and"$num2"
to ensure that variables are properly enclosed in quotes, preventing issues with empty or null variables.Use
-gt
for numeric comparison.
Happy Coding :) πβ¨