Bash Scripting Notebook

Bash scripting is widely used to automate processes, configure systems, perform dev-ops duties, and other things. I've included some fundamental information regarding bash in this post.

Bash Scripting Notebook

Introduction and Basics

Bash is a type of Unix shell command-line interface for interacting with an operating system. Bash scripts are files that the Bash shell uses to run a series of commands. These scripts can be used to configure systems, automate processes, and carry out a variety of other tasks. These scripts have a .sh file extension, all the commands that we wish to run should be included inside the file. 
A basic example of a Bash script that prints "Hello, World!" to the terminal is shown below

#!/bin/bash
echo "Hello, World!"

#! are called shebang characters these are usually put before the shell scripts and then /bin/bash specifies which kind of shell to use this is because we can have multiple types of shells for instance /bin/sh, /bin/zsh, etc. 

To run a Bash script, we need to make the file executable using the chmod command and then run it using the ./ command, like this: 

chmod +x script.sh

./script.sh

some basic concepts in Bash scripting:

  1. Comments: You can add comments to your script by starting a line with the # symbol. Anything following the # on that line will be ignored by the shell. This is useful for adding notes and explanations to your script.

  2. Variables: You can use variables in Bash scripts to store values, such as numbers or strings. To declare a variable, you just need to give it a name and set its value. For example:

          day="Tuesday" temperature=26

To access the value of a variable, you need to use a $ symbol followed by the name of the variable. For example:

        echo "Today is $day and temperature is $temperature degree celcius"
  1. Conditional statements: You can use if statements to execute different blocks of code based on whether a condition is true or false. Here is an example:
if [ "$temperature" = "25" ]
   then
       echo "It is room temperature"
    else
       echo "It is not room temperature"
fi
  1. Loops: You can use for and while loops to execute a block of code multiple times. Here is an example of a for loop:
for i in 1 2 3 4 5
  do
     echo $i
done

And here is an example of a while loop:

count=1
while [ $count -le 5 ]
  do
     echo $count count=$((count + 1))
done

Operators

Arithematic: There are arithmetic operators in bash scripting to facilitate mathematical operations; the most fundamental of them are +, -, *, /, %, and ** which correspond to addition, subtraction, multiplication, division, modulus, and exponentiation respectively.

Comparison operators: These operators determine if a condition is true or false and allow for the comparison of values. There are several different operators for making comparisons: -eq (equal), -ne (not equal), -lt (less than), -le (less than or equal), -gt (greater than), and -ge (greater than or equal).

Logical operators: To determine if a combination conditional is true or false, these operators are used to combine numerous conditions. The three possible logical operators are: && (and), || (or), and ! (not).

String operators: String operations can be performed with these operators equal (=), not equal (!=), empty (-z), and not empty (-n).

File operators: These operators are used to test the properties of files. The file operators are -d (directory), -f (regular file), -L (symbolic link), -r (readable), -w (writable), and -x (executable).

Example:

# ARITHMETIC OPERATORS
# arthmetic operators are: +, -, *, /, %, **
n=100
result=$((n*(n+1)/2))
# OR
# ((result=n*(n+1)/2))
echo "The sum of first $n natural numbers is $result"

# COMPARISON OPERATORS
# comparision operators are: -eq, -ne, -gt, -lt, -ge, -le
n=-10
if [ $n -gt 0 ]
then
    echo "$n is a positive number"
else
    echo "$n is a negative number"
fi

# LOGICAL OPERATORS
# logical operators are: &&, ||, !
n=10
if [ $n -gt 0 ] && [ $n -lt 20 ]
then
    echo "$n is a positive number less than 20"
else
    echo "$n is not a positive number less than 20"
fi

# STRING OPERATORS
# string operators are: =, !=, -z, -n, str
str1="Hello"
str2="World"
if [ $str1 = $str2 ]
then
    echo "$str1 is equal to $str2"
else
    echo "$str1 is not equal to $str2"
fi

# FILE TEST OPERATORS
# file test operators are: -d (directory), -f (regular file), -L (symbolic link), -r (readable), -w (writable), and -x (executable)
file="operators_blog.sh"
if [ -f $file ]
then
    echo "$file is a regular file"
else
    echo "$file is not a regular file"
fi

Output

Arguments, Functions in Bash Scripting

Command Line Arguments: When running a bash script from the command line you can pass the options to it see the figure below:

Parsing Command Line arguments in bash scripting

By adding them after the script name, you can send arguments to your Bash script when you run it. The variables $0, $1, $2, etc., which stand for the script name and the parameters, respectively, can be used in your script to access these arguments.

Writing names_script.sh contents as:

#!/bin/bash

echo "Person 1: $1"
echo "Person 2: $2"
echo "Person 3: $3"

To run the above script we would need to pass 3 arguments like this:

bash names_script.sh Hamid Ali Hussain

it should print:

Person 1: Hamid

Person 2: Ali

Person 3: Hussain

The $* and $@ variables can also be used to retrieve all of the arguments individually or as a single string, respectively. 

Functions: Like in some modern programming languages Bash scripts can have functions. Functions are reusable blocks of code. To define a function, use the function keyword followed by the name of the function and a set of parentheses. For example:

function greet {
  echo "Hello, $1!"
}

To invoke a function, type its name followed by any arguments you want to pass to it. As an example:

greet Hamid

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow