While Loop Example
- While loop syantax:
while control-commands;
- do
other-commands; done
Example:
$ cat while_loop #!/bin/bash i="0" while [ $i -lt 4 ] do echo "this is whole loop $i" i=$i +1 sleep 10 done
- The true control statement will execute the script below until CTRL+C is pressed or the script is killed.
#!/bin/bash while true; do touch PIC-`date +%s`.jpg sleep 10 done
The script in bash -x mode
]$ bash -x while_loop + true ++ date +%s + touch PIC-1184928926.jpg + sleep 10 + true ++ date +%s + touch PIC-1184928936.jpg + sleep 10
Reference: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_02.html