Openunix.eu
Scripting - Loop
- Looping with CASE and multiple options
# Choose new size in MB
select answer in "100M" "150M" "200M"
do
case $answer in
100M ) SIZE=100M;break;;
150M ) SIZE=150M;break;;
200M ) SIZE=200M;break;;
esac
done
- Looping with CASE and Yes/No option
while true;
do
read -p "Do you really want to change the value (Y/N)?" answer
case $answer in
[Yy]* ) answer=ANO;break;;
[Nn]* ) echo "No changes have been applied "; exit 0;;
* ) echo "Press Y or N ";;
esac
done
- Looping with WHILE
# a nice incrementing loop would be:
# notice that for testing math operations doubble rounded brackets work
read -p "Give me a number under 25" x
while true; do
if test (( $x <= 26 )) ;then
echo "we need to increment" && x=$(($x+1))
else
echo "The x variable is already higher then 25" ; break
fi
done
Powered by NetBSD. Running on a toaster.