LDP/LDP/guide/docbook/abs-guide/wh-loopc.sh

35 lines
761 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2001-09-04 13:27:31 +00:00
# wh-loopc.sh: Count to 10 in a "while" loop.
2001-07-10 14:25:50 +00:00
2011-08-29 23:59:19 +00:00
LIMIT=10 # 10 iterations.
2001-07-10 14:25:50 +00:00
a=1
while [ "$a" -le $LIMIT ]
do
echo -n "$a "
let "a+=1"
2011-08-29 23:59:19 +00:00
done # No surprises, so far.
2001-07-10 14:25:50 +00:00
echo; echo
# +=================================================================+
2011-08-29 23:59:19 +00:00
# Now, we'll repeat with C-like syntax.
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
((a = 1)) # a=1
2001-07-10 14:25:50 +00:00
# Double parentheses permit space when setting a variable, as in C.
while (( a <= LIMIT )) # Double parentheses,
2011-08-29 23:59:19 +00:00
do #+ and no "$" preceding variables.
2001-07-10 14:25:50 +00:00
echo -n "$a "
2011-08-29 23:59:19 +00:00
((a += 1)) # let "a+=1"
2001-07-10 14:25:50 +00:00
# Yes, indeed.
# Double parentheses permit incrementing a variable with C-like syntax.
done
echo
2011-08-29 23:59:19 +00:00
# C and Java programmers can feel right at home in Bash.
2001-07-10 14:25:50 +00:00
exit 0