Skip to Content
Learning Linux Shell Scripting
book

Learning Linux Shell Scripting

by Ganesh Sanjiv Naik
December 2015
Beginner
306 pages
5h 2m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting

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 (while loop) or until the condition becomes true (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 it's 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 program:

$ chmod +x while_01.sh ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting - Second Edition

Learning Linux Shell Scripting - Second Edition

Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9781785286216