Working with the do – while loop

Similar to the for command, while is also the command for loop operations. The command next to while is evaluated. If it is successful or 0, then the commands inside do and done are executed.

The purpose of a loop is to test a certain condition or expression and execute a given command while the condition is true (the while loop) or until the condition becomes true (the until loop):

while condition

do

commands

done

until condition

do

commands

done

 

The following is the while_01.sh script in which we read a file and display its content:

#!/bin/bash 
file=/etc/resolv.conf 
while IFS= read -r line    # IFS : inter field separator 
do 
   # echo line is stored in $line 
  echo $line 
done < "$file" 

Let's test the ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.