Posts

Showing posts with the label Shell Scripting

For Loop in Bash Script (Shell Scripting)

Image
Tamizh Tech Tutorial For Loop in Bash Script  (Shell Script) Shell Scripting Tutorial For Loop It is an example of looping statement and alternative for while loop It is used to execute group of statements based on the looping condition Bash supports several examples of for-loops. Some of them below Simple for loop Range based for loop C Style based for loop Infinite for loop. 1. Simple for loop (Data based for loop) Here the loop is based on the data or collection. The data can be the same type or different type. Syntax f or variable in data do # code done Where, variable is an user defined variable. Example 1 - Data based for loop Source Code echo "--------------------------------" echo -e "\t Data based For Loop" echo "--------------------------------" for i in 12 true "Hello" 34.55 do     echo " $i" done Output 2. Range Based for loop Here the loop is based on the three attributes like start, stop and interval . These a

If…elif…else Statement in Shell Script

Image
  If…elif…else Statement This is one of the type of selection statement.   Note It is an important to note that, the simple if, if else and if-elif-else statements should be closed by fi keyword. Syntax if [ condition ] then      true statement elif [ condition ] then      true statement else      false statement fi Example 1 - Shell Style Code Biggest of Three Numbers Source Code echo "Enter the inputs: " read a b c if [ $a -gt $b -a $a -gt $c ]          # -a indicates logical AND then     echo "$a is big" elif [ $b -gt $c ] then     echo "$b is big" else     echo "$c is big" fi Output NOTE It is not possible to directly use logical operator symbols in shell script. The statement below the shows the example Logical Operators     Operator                                Operators in Shell         Logical AND                    &&                   -a     Logical OR                              ||                         -o

While Loop in Shell Script Examples

Image
WHILE LOOP IN SHELL SCRIPT While Loop It is an example of looping statements or iteration statements It is used to execute the statements , if the condition is true otherwise it will skip the loop It is an alternative for normal for loop. Syntax      while [ condition ]       do true statement      done Types of Styles Shell Style C Style Shell Style  Here, we have to use the default shell style of syntax for writing the expression in the shell script. C Style in Shell Script Here, we can write code for expression using c language style using double braces ((expression)) 1. C STYLE BASED FACTORIAL CODE Source Code       echo "--------------------------"      echo -e "\t Factorial Program"      echo "--------------------------"      echo "Enter a Number: "      read n # define the variable      f=1      i=1 # define the while loop      while ((i<=n))       do          f=$((f*i))          ((i++))      done      echo "Fact is: $f" Ou

Shell Script If Statement

Image
  Shell Script If Statement If Statement In shell script, if statement is used to execute a single condition or rule If the condition is true , then true statements will be executed otherwise false statements will be executed. Syntax          if [ condition ]         then                true statements         else                false statements         fi Note The keyword fi should be used to complete the if syntax. Example (p1.sh) Source Code      echo "--------------------------------"      echo -e "\t If Statement Example"      echo "--------------------------------"      echo "Enter a number: "      read n # get a remainder of base 2 using c style code      r=$((n%2)) # find the odd or even number using remainder option      if [ $r -eq 0 ]      then          echo "This is Even Number"      else          echo "This is Odd Number"      fi Output Video Demo