LDP/LDP/guide/docbook/abs-guide/ex28.sh

50 lines
762 B
Bash
Raw Permalink Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
LIMIT=19 # Upper limit
echo
echo "Printing Numbers 1 through 20 (but not 3 and 11)."
a=0
while [ $a -le "$LIMIT" ]
do
a=$(($a+1))
2005-05-08 20:09:31 +00:00
if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # Excludes 3 and 11.
2001-07-10 14:25:50 +00:00
then
2005-05-08 20:09:31 +00:00
continue # Skip rest of this particular loop iteration.
2001-07-10 14:25:50 +00:00
fi
2005-05-08 20:09:31 +00:00
echo -n "$a " # This will not execute for 3 and 11.
2001-07-10 14:25:50 +00:00
done
2002-04-01 16:04:17 +00:00
# Exercise:
2008-11-23 22:43:47 +00:00
# Why does the loop print up to 20?
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
echo; echo
2001-07-10 14:25:50 +00:00
echo Printing Numbers 1 through 20, but something happens after 2.
##################################################################
# Same loop, but substituting 'break' for 'continue'.
a=0
while [ "$a" -le "$LIMIT" ]
do
a=$(($a+1))
if [ "$a" -gt 2 ]
then
2001-09-04 13:27:31 +00:00
break # Skip entire rest of loop.
2001-07-10 14:25:50 +00:00
fi
echo -n "$a "
done
2001-09-04 13:27:31 +00:00
echo; echo; echo
2001-07-10 14:25:50 +00:00
exit 0