Posts tagged Infinite loop example in Bash shell scripting
How to write infinite loop in Bash using for or while statement
Oct 17th
The while-do-done statement can be used to program an infinite loop in bash. The following sample shows how it can be done.
#!/bin/bash while [ 1 ]; do echo I am in infinite loop. Press Ctrl+C to stop me done
The break statement can be used to exit the while loop. The following sample code breaks away from while loop after iterating 10 times.
#!/bin/bash
i=1
while [ 1 ];
do
i=`expr $i + 1`
echo loop $i
if [ "$i" == "10" ]; then
echo Exiting while loop after iterating $i times
break
fi
done
echo end of shell script
The following will be the output More >


Recent Comments