Bash Script

Bash Script

Condition

  • if
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    if [ expression ];
    # ^ ^ ^ please note these spaces
    then
    statement(s)
    fi

    if [ expression ] && [ expression_2 ]
    if [ expression ] || [ expression_2 ]
    # following if syntax is allowed. Please observe that the condition has double square brackets
    if [[ expression_1 && expression_2 || expression_3 ]];
    • -gt (greater than)
    • -lt (less than)
    • -eq (equal to)
    • -ne (not equal to) – [ -a FILE ] (Depreciated method to check in bash if file exists.)
      – [ -e FILE ] (Preferred method to check in bash if file exists.)
      – -z (to check if string has zero length)
      – -s (to check if file size is greater than zero)
      – -n (to check if string length is not zero)
      – -f (to check if file exists and is a regular file)
    • -x (to check if file is executable)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      #!/bin/bash

      # Scenario - File exists
      if [ -e /home/tutorialkart/sample.txt ];
      then
      echo "sample.txt - File exists."
      else
      echo "sample.txt - File does not exist."
      fi

      if [ -z "hello" ]
      if [ -s /home/tutorialkart/sample.txt ]

Reference