Linux Admin - Loop Control
Sometimes (a lot times actually) we will either want to skip loop iteration operation or break out of a loop prior to completion. These operations are performed with the following verbs: continue and break.
continue
#!/bin/bash
myFile = "myLines.txt"
while read -a FILENAME;
do
if [ `echo $FILENAME | grep 004` ];
then
continue
fi
echo $FILENAME
done < $myFile
The snippet above will skip the 4th line of the text file, then continue script execution.
line001 line002 line003 line005 line006 line007 line008 line009
break
Break will stop the loop in its entirety rather than skip a single iteration when a condition is met.
#!/bin/bash
myFile = "myLines.txt"
while read -a FILENAME;
do
if [ `echo $FILENAME | grep 004` ];
then
break
fi
echo $FILENAME
done < $myFile
The above script produces the following output.
line001 line002 line003
linux_admin_shell_scripting.htm
Advertisements