Shell Scripting Tutorial for Beginners

I am sharing few beginner shell scripting reference that we need in day-to-day tasks.

Local Variable in Shell Script

We can create local variables in shell script by using the keyword local.

#!/bin/bash
HELLO=Hello
function helloWorld {
  local HELLO=World
  echo $HELLO
}
echo $HELLO
helloWorld
echo $HELLO

Conditional Statement

1) if conditional statement

The base for the ‘if’ constructions in bash is this:
Sytanx:

if [expression];
then
   code if 'expression' is true.
fi

Example:

#!/bin/bash
if [ "foo" = "foo" ]; then
   echo when condition is true
fi

2) if .. then … else conditional statement

Example:

#!/bin/bash
if [ "bar" = "bar" ]; then
   echo when condition is true
else
   echo when condition is false
fi

Example:

#!/bin/bash
VAR1="value1"
VAR2="value2"
if [ "$VAR1" = "$VAR2" ]; then
	echo when condition is true
else
	echo when condition is false
fi

List of String Comparison , Numeric Comparison and File Check Condition used in Shell Scripting.

String ComparisonDescription
String1 = String2Returns true if the strings are equal
String1 != String2Returns true if the strings are not equal
-n String1Returns true if the string is not null
-z String1Returns true if the string is null
Numeric ComparisonDescription
expr1 -eq expr2Returns true if the expressions are equal
expr1 -ne expr2Returns true if the expressions are not equal
expr1 -gt expr2Returns true if expr1 is greater than expr2
expr1 -ge expr2Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2Returns true if expr1 is less than expr2
expr1 -le expr2Returns true if expr1 is less than or equal to expr2
! expr1Negates the result of the expression
File ConditionalsDescription
-d fileTrue if the file is a directory
-e fileTrue if the file exists (note that this is not particularly portable, thus -f is generally used)
-f fileTrue if the provided string is a file
-g fileTrue if the group id is set on a file
-r fileTrue if the file is readable
-s fileTrue if the file has a non-zero size
-uTrue if the user id is set on a file
-wTrue if the file is writable
-xTrue if the file is an executable

Conditional Statement: Loops for, while and until

for: for loops iterate through a set of values until the list is exhausted:
while: The while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code.)
until: The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.

For Loop:

Syntax:

for var in 
do
  #command to perform some operation based on list item
done

Example:

#!/bin/bash
# Basic for loop
TAGLINE='Techieroop Solution for DevOps'
for word in $TAGLINE
do
  echo $word
done

Output:

Techieroop
Solution
for
DevOps

Iterate over ranges

#!/bin/bash
# Range in for loop
for value in {1..5}
do
  echo $value
done

Output:

1
2
3
4
5

While Loop

While loop is executed as long as given condition is true.

#!/bin/bash 
COUNTER=0
while [  $COUNTER -lt 10 ]; do
 echo The counter is $COUNTER
 let COUNTER=COUNTER+1 
done

Until Loop

The until loop is fairly similar to the while loop. The difference is that it will execute the commands within it until the test becomes true.

#!/bin/bash 
COUNTER=20
until [  $COUNTER -lt 10 ]; do
 echo COUNTER $COUNTER
 let COUNTER-=1
done
(Visited 118 times, 3 visits today)