Contents /
Previous /
Next
Control Statements
break
When a loop may require premature termination
you can include the break statement within it:
#!/bin/sh
for i in 0 1 2 3 4 5 6 7 8 9 ; do
NEW_FILE=$1.BAK-$i
if test -e $NEW_FILE ; then
echo "backup-lots.sh: **error** $NEW_FILE"
echo "already exists - exiting"
break
else
cp $1 $NEW_FILE
fi
done
continue:
If a continue statement is encountered, execution
will immediately continue from the top of the loop,
thus ignoring the remainder of the body of the loop:
#!/bin/sh
for i in 0 1 2 3 4 5 6 7 8 9 ; do
NEW_FILE=$1.BAK-$i
if test -e $NEW_FILE ; then
echo "backup-lots.sh: **warning** $NEW_FILE"
echo " already exists - skipping"
continue
fi
cp $1 $NEW_FILE
done
both break and continue work inside for,
while, and until loops.