1.

Write down the Syntax for all the loops in Shell Scripting.

Answer»

In shell scripting, you can USE three looping statements as given below:

  • while statement
  • for statement
  • until statement

Syntax is as follows:  

  • For Loop: Loops that operate on LISTS of items are known as loops. Each item in the list receives the same set of commands.

Syntax: 

 for VAR in word1 word2 ... wordN do Statement to be executed done

Example: 

for a in 1 2 3 4 5 6 7 8 9 10 do if [ $a == 3 ] then break fi echo "Iteration no $a" done

Output” 

$bash -f main.sh Iteration no 1 Iteration no 2
  • While Loop: It involves evaluating a command first and then executing a loop based on the result. The loop will be terminated if the command raises to FALSE.

Syntax:  

while command do Statement to be executed done

Example:  

a=0 while [ $a -lt 3 ] do echo $a a=`expr $a + 1` done

Output: 

$bash -f main.sh 0 1 2 3
  • Until Loop: As OFTEN as the condition/command evaluates to false, the until loop is executed. Once the condition or command becomes true, the loop terminates.

Syntax: 

until command do Statement to be executed until command is true done

Example: 

a=0 until [ $a -gt 3 ] do echo $a a=`expr $a + 1` done

Output: 

$bash -f main.sh 0 1 2 3


Discussion

No Comment Found