BASH Scripting

What is it

  • A set of commands that can be executed in terminal
  • Should begin with the shebang line (#!/bin/bash)
  • Bash scripts ends with .sh

Variables

  • Store data and configuration options
  • Declared with name followed by "=" and the value
  • Call: Put $ in front of the variable name, enclose the name in {} if directly followed by something else


Examples


#!/bin/bash
a=/home/web
echo "My home folder is $a"
echo "My data folder is ${a}/data"
                            

Arrays

  • Arrays are variables containing multiple values
  • Declared with name followed by "=()". Values are space separated.
  • Values can be accessed by their index starting from 0

Example


#!/bin/bash
# declare a array variable color
color=(red blue green)
# print all elements of the array
echo ${color[*]}
# set 4th element of the array
color[3]=yellow
echo ${color[*]}
# print third element of the array
echo "The sunflower is ${color[2]}"
# print the number of elements in array
echo ${#color[*]}
                            

Variable Expansion


Variable names are exapnded within double quotes, but not within single quotes.

Example


#!/bin/bash
string="abcdefg"
echo "$string"
echo "\$string"
echo '$string'

echo $'\U1f60e'
                            

For Loop

Repetition



for((i=1; i<=5; ++i)); do
    echo ${i}
done
                            

Functions


#!/bin/bash
function greeter() { echo "Hello ${USER}!"; }
greeter
                            

#!/bin/bash
function greeter() { echo "Hello $1!"; }
greeter "My Friend"
                            

Testing Conditions

  • [ ! <expression> ] # expression is false
  • [ <expr1> -a <expr2> ] # both are true
  • [ <expr1> -o <expr2> ] # either is true
  • [ -n <string> ] # string length is nonzero
  • [ -z <string> ] # string length is zero
  • [ <string1> = <string2> ] # strings are equal
  • [ <string1> != <string2> ] # strings are not equal

Testing Conditions

  • [ <int1> -eq <int2> ] # integers are equal
  • [ <int1> -ne <int2> ] # integers are not equal
  • [ <int1> -lt <int2> ] # int1 is less than int2
  • [ <int1> -le <int2> ] # int1 is less or equal int2
  • [ <int1> -gt <int2> ] # int1 is greater than int2
  • [ <int1> -ge <int2> ] # int1 is greater or equal int2

Testing Conditions

  • [ -e <file> ] # file exists
  • [ -f <file> ] # file exists and is a regular file
  • [ -L <file> ] # file exists and is a symbolic link
  • [ -d <file> ] # file exists and is a directory
  • [ -s <file> ] # file exists and has size greater than 0
  • [ -r <file> ] # file exists and is readable
  • [ -x <file> ] # file exists and is executable

Control Structures: if/elif/else/fi


#!/bin/bash
function f() {
    if [ $1 -lt 0 ]; then
        echo "negative"
    elif [ $1 -le 9 ]; then
        echo "one digit"
    elif [ $1 -le 99 ]; then
        echo "two digits"
    else
        echo "large!"
    fi
}

f 21345890
                            

While Loop

Repetition



#!/bin/bash

count=10
i=0

while [ $i -lt $count ]; do
   echo "$i"
   i=$(($i+1))
done
                            

This Presentation

It will be available on Github. Refer for any doubts!

THE END

Give us a Feedback!

Reference Book

- The Linux Command Line

let's go on