DEV Community

Dharamraj Yadav
Dharamraj Yadav

Posted on

My First Bash Scripts: From Zero to Production-Quality

What a bash script is

Bash script is a box of commands that used to run multiple commands at a time.

The Shebang (#!/bin/bash)

This is the very first line of the script that determine the shell, and based on the it'll execute the commands.

Making a script executable

When you create a script file you didn't have execute permission on that script file so you need to make that executable.

chmod u+x filename.sh
Enter fullscreen mode Exit fullscreen mode

Variables

You can create variable like this

name="Dharam"
Enter fullscreen mode Exit fullscreen mode

Make sure in the whole line don't have space if you add space anywhere that is not variable, and you can use the variable using the $ sign.

echo $name
Enter fullscreen mode Exit fullscreen mode

And variable names are case-sensitive so if you use capital or small letter both are different variables.

name="Dharam"
Name="Aadi"
Enter fullscreen mode Exit fullscreen mode

if you want to use the variable with text you we have double quote or single quote

Double Quote

name="Dharam"
echo "Hello my name is $name"
## it print this **Hello my name is Dharam**
Enter fullscreen mode Exit fullscreen mode

when you use double quote it mean the doller sign represent the variable.

Single Quote

name="Dharam"
echo 'Hello my name is $name'
## it print this "Hello my name is $name"
Enter fullscreen mode Exit fullscreen mode

Single quote use as a literal.

Arithmetic

When you want to do arithmetic operation you need to use

$((  ))
Enter fullscreen mode Exit fullscreen mode

Conditions

Age determine script

#!/bin/bash
age=18

if [ "$age" -gt 20 ]
then
     echo "You are an adult"
fi
Enter fullscreen mode Exit fullscreen mode

This is a very very simple conditions it only determine one condition. if you want to add multiple condition you need to use elif.

#!/bin/bash
marks=75

if [ "$marks" -ge 80 ]
then
     echo "You got a first division"
elif [ "$marks" -ge 60 ]
then
     echo "You got a second division"
elif [ "$marks" -ge 38 ]
then
     echo "You got a third division"
else
     echo "You are fail!"
fi
Enter fullscreen mode Exit fullscreen mode

Condition Number comparisons

-ge, -gt, -le, -lt, -eq, -ne
Enter fullscreen mode Exit fullscreen mode

case, esac and *) also use to for one value.

Loops

For Loop

#!/bin/bash

for num in {1..10}
do
    echo $num
done

Enter fullscreen mode Exit fullscreen mode

This will print 1 to 10. for loop used to a fixed range we know the range.

While Loop

#!/bin/bash

count=0

while [ $count -lt 10 ]
do
    echo $count
    count=$(( count + 1 ))
done
Enter fullscreen mode Exit fullscreen mode

While loop used to we don't know when the condition fullfill.

Until Loop

#!/bin/bash
count=1

until [ $count -gt 10 ]
do
    echo $count
    count=$(( count + 1 ))
done
Enter fullscreen mode Exit fullscreen mode

The count loop is opposite of the while loop, it stops when the condition become true.

Functions

#!/bin/bash

name="Dharam"

greet(){
    echo "Hello My name is $name"
}

greet
Enter fullscreen mode Exit fullscreen mode

The functions are used to do some certain tasks. and we can also pass the arguments to the functions:-

#!/bin/bash

greet(){
    echo "Hello My name is $1"
    echo "I'm learning the $2"
}

greet Dharam DevSecOps
Enter fullscreen mode Exit fullscreen mode

The $1, $2 are the arguments value.

Error Handling (Production-Quality)

Remember we have two type of code one is 0 and second is except 0, 0 mean command run succeed properly if the exit code is not 0 it mean command failed.

Exit Code

if you want to see your last command exit code you can use the $? this'll shows the last command exit code

echo $?
Enter fullscreen mode Exit fullscreen mode

&& And ||

&&

This used to if the first command is run with exit code 0 then run second one otherwise not.

cd /folder/not/exists && rm -rf *
Enter fullscreen mode Exit fullscreen mode

if we didn't use the && and the command before && fails the second command also run, so we use the && to prevent second command to run if first command is fail.

||

This used to if the command is failed then run the second one.

cd /myfol || echo "Do not entered in the folder"
Enter fullscreen mode Exit fullscreen mode

set -e

This is very important to use in bash script.

#!/bin/bash

set -e

echo "First Command"

cd /folder/not/exists

rm -rf *
Enter fullscreen mode Exit fullscreen mode

The set -e stop the script if the any command failed. this is lifesaver.

set -u

Stop the script if the variable is undefined. use it like set -e.

set -o pipefail

set -o pipefail makes the whole pipe count as failed if any command in it fails (not just the last). So the failure shows up in the exit code instead of being hidden.

set -euo pipefail

You can use the all options together.

trap

This mean run the command if the script fail or succeed, it is very helpful when we want to clear something if you script fail or succeed. and also if you press ctrl c then also it run.

Top comments (0)