#!/bin/sh -u # $0 (no args) # A script that loops printing integers, first up and then down. # Some of the comments in this script are "teacher" comments that should # never appear in student programs (or real-world programs). # -IAN! idallen@ncf.ca February 2002 export PATH=/bin:/usr/bin umask 022 # Start and end of loop. # start=1 end=5 increment=1 # 1. Count UP. # Purely numeric variables don't need quotes when used, because they don't # ever contain shell special characters (such as wildcards). You can quote # them anyway, to remind yourself that [almost] all variables should be quoted. # i="$start" while [ "$i" -le "$end" ]; do echo "i is $i" let i=i+increment sleep 1 done # 2. Count DOWN. # Trick: We put a minus sign in front of the positive increment value # to enable the loop to count downward. Note that the while loop itself # is almost identical code to the loop code above. With one small change, # the two loops could be made into identical code and written ONCE. # Less code is better code! # i="$end" increment="-$increment" # reverse the increment (make negative) while [ "$i" -ge "$start" ]; do echo "i is $i" let i=i+increment sleep 1 done exit 0