Can you provide me a while loop control flow statement shell script syntax and example that allows code to be executed repeatedly based on a given boolean condition?
Each while loop consists of a set of commands and a condition. The general syntax as follows for bash while loop:
while [ condition ] do command1 command2 commandN done
- The condition is evaluated, and if the condition is true, the command1,2…N is executed.
- This repeats until the condition becomes false.
- The condition can be integer ($i < 5), file test ( -e /tmp/lock ) or string ( $ans != "" )
ksh while loop syntax:
while [[ condition ]] ; do command1 command1 commandN done
csh while loop syntax:
while ( condition ) commands end
BASH while Loop Example
#!/bin/bash c=1 while [ $c -le 5 ] do echo "Welcone $c times" (( c++ )) done
KSH while loop Example
#!/bin/ksh c=1 while [[ $c -le 5 ]]; do echo "Welcome $c times" (( c++ )) done
CSH while loop Example
#!/bin/csh c=1 while ( $c <= 5 ) echo "Welcome $c times" @ c = $c + 1 end
#!/bin/csh set yname="foo" while ( $yname != "" ) echo -n "Enter your name : " set yname = $< if ( $yname != "" ) then echo "Hi, $yname" endif endTwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop