Monday, January 31, 2011

BASH : While, Until Loops

While Loop - tests at the begining to see if the exit status is zero; Loop while true and when becomes false stop
CONTENT OF FILE
#!/bin/bash
NUM=0
MAX=10
while [ "$NUM" -lt "$MAX" ] #while [ "$NUM" -le "$MAX" ] #while [ "$NUM" -gt "$MAX" ]
do
echo $NUM
let "NUM += 1"
done
#END
Result -> count from 0 to 10
while ["``"] -> while loop search for a status. If the status is 0 continue to work else will finish


Until Loop - tests until the exit status is zero; Loop while false and when becomes true stop
touch untillloop.sh
CONTENT OF FILE
#!/bun/bash
NUM=100
MIN=20
#until [ condition-is-true]; do command done
until [ "$NUM" -eq "$MIN" ]
do
echo $NUM
let "NUM -= 1"
#END
Results -> count from 100 from 20
CONTENT OF FILE
#!/bun/bash
until [ "$STATUS" -eq "0" ]
do
ping -c 1 192.168.1.111 #bad host
echo The host is down
STATUS=`echo $?`
#END

No comments:

Post a Comment