Skip to main content

Command Palette

Search for a command to run...

โš™ Automate Linux with Shell Scripting ๐Ÿง๐Ÿ‘จโ€๐Ÿ’ป

Basic to Advance for DevOps Engineer or Developer

Updated
โ€ข11 min read
โš™ Automate Linux with Shell Scripting ๐Ÿง๐Ÿ‘จโ€๐Ÿ’ป

๐Ÿ‘‰ Quick Intro ๐Ÿ”ฅ

  • Shell is the intermediate or medium of communication between applications/users and Hardware

    The Difference Between the Shell and the Terminal - DEV Community

  • To automate Linux commands, along with some condition-based or repetitive tasks, requires writing a few lines of code, which is termed as Shell Scripting

๐Ÿ‘‰ Types of shell โš™

TypeYear
Bourne shell (sh)1977
C shell (csh)1970
KornShell (ksh)1980
Bourne Again Shell (bash)1989
Z shell (zsh)1990
  • Most preferred is bash in Linux and zsh in mac

  • You can see your shell type using $0

๐Ÿ‘‰ How to create shell script โ“

  • Create a file with any name but the extension should be .sh

  • Open the file and write the below code in it

      #!/bin/bash
    
      echo "Hello World"
    
  • First line that you see is called the shebang line, It's like set of instructions that computer reads at beginning of script to know which interpreter should be used to execute script

  • To print anything on the terminal the echo command is used

  • After saving the file, you need to give execute permission to file to execute it

  • Now you can execute the script by ./filename.sh

๐Ÿ‘‰ Variables

  • It is like an object that can store different types of data like, integer, float, string, and boolean

  • To declare the variable see the below code

    var=value

    OR

    declare var=value

    OR

    declare -i var=value

    -i for integer variable

  • To display variables there are two syntax

    echo $variable

    echo ${variable}

      #!/bin/bash
    
      # string
      name="John"
      # integer
      age=27
      # float
      height=5.3
      # boolean
      isEligible=true
    
      # you can display value of variables by two ways
      echo "$name"
      echo "$age"
      echo "${height}"
      echo "${isEligible}"
    
  1. Few rules to declare a variable & their names ๐Ÿ“

    • No space around =

    • Variable name must start with a letter or underscore

    • Number can be used anywhere else but not at the start

    • DO NOT USE special characters like @, #, %

  • Case sensitive

  1. Input ๐Ÿง‘โ€๐Ÿ’ป

    • To take input from the user we use read

        #!/bin/bash
      
        echo "Enter name"
        read name
        echo "Your name = ${name}"
      
    • In the above code, first it will print "Enter name" on a terminal and then will wait for the user to enter a name, after entering the name press "Enter", On the next line "Your name = xyz" will be present

    • You can take all types of data as an input

    • You can combine display message for input and taking input into single line as below

        #!/bin/bash
      
        read -p "Enter name = " name
        echo "Your name = ${name}"
      
  1. Output ๐Ÿ–ฅ๏ธ

    • To display anything on the terminal we have two command echo and printf

    • echo we have already seen it

    • Let's see printf

      printf <output-with-format> <variable>

        #!/bin/bash
      
        name="John"
        age=27
        height=5.321
      
        printf "Name = %s\n" "$name"
        printf "Age = %d\n" "$age"
        printf "Height = %f\n" "$height"
      
    • \n is for a new line

    • By default decimal number will print 6 digits after the dot, to limit it you can use the below types

        #!/bin/bash
      
        pi=3.14159
        printf "%10.2f\n" "$pi"
      

๐Ÿ‘‰ Comments

Code comments are like little notes that programmers write in their code to explain what it does. It helps them and other people understand the code better.

It is ignored by the Interpreter.

๐Ÿ‘‰ Constants

  • Constants are variables whose value can not be changed once declared

    readonly var=value

    OR

    declare -r var=value

      #!/bin/bash
    
      readonly name="John"
      echo "Your name = ${name}"
      # below line will give error as name is readonly
      name="Paul"
      echo "Your name = ${name}"
    

๐Ÿ‘‰ Command Line Arguments

  • CLA are one type of variables which we pass to the script while executing it

    ./file.sh var1 var2 var3 ...

  • We can pass n number of CLA

  • To use these variables in script we can use the below notations

      #!/bin/bash
    
      echo "File name = $0"
      echo "First argument = $1"
      echo "Second argument = $2"
      echo "Third argument = ${3}"
      echo "All arguments = $*"
      echo "All arguments = $@"
      echo "Count of arguments = $#"
    

  • echo "Count of arguments = $#"

  • shift is used to remove the first few CLA

      #!/bin/bash
    
      # remove first CLA
      shift 1
      echo "$*"
    
      # remove starting 3 CLA
      shift 3
      echo "$*"
    

๐Ÿ‘‰ Array

  • An array is a set of variables

  • Can contain different types of data

  • It has different syntax for different operations

      #!/bin/bash
    
      # array contains four types of data
      arr=(91 "john" true 3.14)
    
      # display all array items
      echo "Array = ${arr[*]}"
      echo "Array = ${arr[@]}"
    

Array Operations โš™

  • Indexing starts from 0

  • Element at index, all elements, array length, slicing

      #!/bin/bash
    
      arr=(91 "john" true 3.14)
    
      echo "Element at index 2 = ${arr[2]}"
      echo "Array elements = ${arr[*]}"
      echo "Array elements = ${arr[@]}"
      echo "Length of array = ${#arr[*]}"
      echo "Length of array = ${#arr[@]}"
      echo "2 elements from 1 = ${arr[@]:1:2}"
    

  • Add elements to an array, Remove elements, or Delete array

      #!/bin/bash
    
      arr=(91 "john" true 3.14)
      echo "Array = ${arr[*]}"
    
      # add 4 and Hi at end of array
      arr+=(4 "Hi")
      echo "Array = ${arr[*]}"
    
      unset arr[2]
      echo "Removed element at 2 = ${arr[*]}"
    
      unset arr
      echo "Remove all elements = ${arr[*]}"
    
  • Copy one array to another, concatenate two arrays

      #!/bin/bash
    
      arr=(91 "john" true 3.14)
    
      # * or @ both will work
      new_arr=(${arr[*]})
      echo "New Array = ${new_arr[*]}"
    
      concatenated_arr=(${arr[@]} ${new_arr[@]})
      echo "Concatenated Array = ${concatenated_arr[*]}"
    

๐Ÿ‘‰ Arithmetic Operators ยฑ

  • To evaluate arithmetic operations there are three ways

    $((...))

    $[...]

    let result=...

      #!/bin/bash
    
      a=2
      b=4
    
      echo "Addition = $(( $a + $b ))"
      echo "Division = $[ $a / $b ]"
      let c=$a**$b
      echo "Exponent = $c"
    
  • You can see for division only 0 is printed not the entire decimal number

  • To work with decimal numbers we need an external calculator i.e bc

  • bc can be used for all calculations not only decimals

      #!/bin/bash
    
      echo "3.8 + 4.2" | bc
      bc <<< "1.5 * 3.3"
      echo "scale=3; 2/5" | bc
      bc <<< "scale=3; 4/2"
    

  • To increment/decrement integer numbers you can use the below ways

    let c+=1

    let c++

    let c-=1

    let c--

๐Ÿ‘‰ String Operations & Operators

  • Suppose you have a string variable named str

  • String has six main operations

      #!/bin/bash
    
      message="Hello John, Good Morning"
    
      echo "Length = ${#message}"
      echo "UPPERCASE = ${message^^}"
      echo "lowercase = ${message,,}"
      echo "Name = ${message:6:4}"
      echo "Replace John with Paul = ${message/John/Paul}"
      echo "Replace o with O = ${message//o/O}"
    

  • String has four main operators

    These operators will be used with if to check the conditions

    == => Check if two strings are equal

    != => Check if two strings are not equal

    -z => Check if a string is zero length or null

    -n => Check if a string is non-zero length

๐Ÿ‘‰ Integer Operators ๐Ÿ”ข

  • These operators will be used with if to check conditions

    -eq => Equal

    -ne => Not Equal

    -gt => Greater than

    -lt => Less than

    -ge => Greater than or equal to

    -le => Less than or equal to

๐Ÿ‘‰ File Operators ๐Ÿ—ƒ

  • These operators will be used with if to check the conditions

    -e => Tests if the file exists.

    -f => Tests if the file is a regular file (not a directory or a device file).

    -d => Tests if the file is a directory.

    -s => Tests if the file is not zero size (i.e., it has some content).

    -r => Tests if the file has read permission.

    -w => Tests if the file has write permission.

    -x => Tests if the file has execute permission.

๐Ÿ‘‰ If Else โคณ

  • There are three variants of if else
  1. If

    • Code inside "if" is executed if the condition is true otherwise it is skipped

        #!/bin/bash
        name1="John"
        name2="John"
      
        if [[ $name1 == $name2 ]]
        then 
              echo "Both names are equal"
        fi
      
        # -z is used to check if string is empty or null
        # below code will be skipped because name is not empty
        if [[ -z $name1 ]]
        then
            echo "name is empty"
        fi
      

  1. If else

    • code inside "if" is executed if the condition is true otherwise code inside "else" is executed

        #!/bin/bash
      
        file_path="/home/ubuntu/demo.txt"
      
        # check if file exists
        if [[ -e "$file_path" ]]
        then
            echo "File exists."
        else
            echo "File does not exist."
        fi
      
        # check if file is directory
        if [[ -d "$file_path" ]]
        then
            echo "Directory exists."
        else
            echo "Directory does not exist."
        fi
      
        # check if file has write permission
        if [[ -w "$file_path" ]]
        then
            echo "File has write permission."
        else
            echo "File does not have write permission."
        fi
      

  1. If elif else

    • it has multiple conditions with code for each condition

        #!/bin/bash
      
        num1=10
        num2=20
      
        # print largest number
        if [[ $num1 -gt $num2 ]]
        then
            echo "$num1 is greater"
        elif [[ $num1 -lt $num2 ]]
        then
            echo "$num2 is greater"
        elif [[ $num1 -eq $num2 ]]
        then
            echo "Both are equal"
        fi
      

๐Ÿ‘‰ Loops โ†ป

  • A loop is a block of code that keeps on executing until the provided condition becomes false.

  • There are main three types of loops, For, While and Until

  1. For

    • There are three parts in for loop

    • Initialization - Initialize variable value with some data

    • Condition - Run for loop until the condition is true

    • Updation - Increase or decrease the value of an initialized variable

    for (( Initialization ; Condition ; Updation ))
    do
        ...
    done
    #!/bin/bash

    # print 1 to 10
    for(( i=1 ; i<=10 ; i++ ))
    do
        echo $i
    done

  • To run for loop infinite times don't give condition

      #!/bin/bash
    
      # print 1 to infinity till system breaks
      for(( i=1 ; ; i++ ))
      do
          echo $i
      done
    
  1. For in

    • This type of for loop is used to iterate over lists or arrays

        #!/bin/bash
      
        # list
        for i in "John" "Paul" 123
        do
            echo $i
        done
      
        # array
        arr=(91 "john" true 3.14)
      
        for i in ${arr[*]}
        do
            echo $i
        done
      

  1. While

    • while loop will keep on executing till the condition become false

        Initialization
        while [[ condition ]]
        do
            ...
            Updation
        done
      
        #!/bin/bash
      
        i=1
        while [[ $i -le 10 ]]
        do
            echo $i
            let i++
        done
      

  • To execute while loop infinite times instead of condition give true

      #!/bin/bash
    
      i=1
      while [[ true ]]
      do
          echo $i
          let i++
      done
    
  1. Until

    • Until loop works exact opposite of the while loop

    • It will keep on executing till the condition becomes true

    • Everything else is the same as while loop

    • For infinite until loop we give false instead of condition

๐Ÿ‘‰ Break and Continue ๐Ÿ’”

  • If you want to stop the loop if some particular condition or state is reached then break is used

      #!/bin/bash
    
      arr=(91 "john" true 3.14)
    
      # break loop after printing john
      for i in ${arr[*]}
      do
          echo $i
          if [[ $i == "john" ]]
          then
              break
          fi
      done
    

  • If you want to skip a particular iteration of the loop continue is used

      #!/bin/bash
    
      arr=(91 "john" true 3.14)
    
      # break loop after printing john
      for i in ${arr[*]}
      do
          if [[ $i == "john" ]]
          then
              continue
          fi
          echo $i
      done
    

๐Ÿ‘‰ Case โ”‹

  • If we have multiple elif conditions then case is most suitable to use

      case expression in
        pattern1)
          # commands to execute if expression matches pattern1
          ;;
        pattern2)
          # commands to execute if expression matches pattern2
          ;;
        pattern3)
          # commands to execute if expression matches pattern3
          ;;
        *)
          # default case if nothing matches
          ;;
      esac
    
  • expression is the variable you want to test against multiple patterns, expression variable can be of any type

  • pattern1), pattern2), etc.: These are the patterns you want to match against the expression. Each pattern should end with a closing parenthesis.

  • ;; => This is used to terminate each pattern block. If a pattern matches, the commands for that pattern are executed, and then the case statement exits. The ;; serves as a break.

  • *) => This is a default case if no pattern matches.

      #!/bin/bash
    
      read -p "Enter extension: " extension
    
      case "$extension" in
        "txt")
          echo "Text file."
          ;;
        "jpg" | "jpeg" | "png")
          echo "Image file."
          ;;
        "sh")
          echo "Shell script."
          ;;
        *)
          echo "Unknown file type."
          ;;
      esac
    

๐Ÿ‘‰ Function โš™

  • functions are small codes, which can be called with a single function name n numbers of times as per requirement

      # preferred way
      function functionName() {
          ...
      }
    
      OR
    
      function functionName {
          ...
      }
    
      OR
    
      functionName() {
          ...
      }
    
  • To pass arguments to function see below code

      #!/bin/bash
    
      # pass arguments to function
      function functionName() {
          echo "Name = $1"
          echo "Age = $2"
      }
    
      functionName "John" 22
    
  • All Command Line Arguments can't be used in functions unless we pass them as parameter

  • To use functions from different scripts, source can be used

  • At the top of a file do source ~/anotherFile.sh

  • To declare local variable in function see below code
#!/bin/bash

function functionName() {
    # this variable is only accessible in function
    local name="Paul"
    echo $name
    echo "Name = $1"
    echo "Age = $2"
}

functionName "John" 22

๐Ÿ‘‰ Logger ๐Ÿšฉ

  • To log anything from bash you can use logger

      #!/bin/bash
    
      logger "This is log"
    
  • You can find this log at the bottom of /var/logs/syslog

๐Ÿ‘‰ Conclusion โœ…

This much is more than enough for DevOps

Happy Learning !!! ๐Ÿ‘‹