While Loop
A while loop is executed as long as a given condition is true.
Here we will learn the use of while loops.
While Loop in Linux Shell Scripting
Syntax:
while [ condition ] do { code to execute } .... done
Example: while.sh
#!/bin/sh # Get the value from the user echo " Enter a value for the variable x:" read x while [ $x -le 10 ] do echo "The value of x is $x" x=`expr $x + 1` done
Result:
./while.sh
Enter a value for the variable x: 6 The value of x is 6 The value of x is 7 The value of x is 8 The value of x is 9 The value of x is 10
In the above example, the value of x is 6.
Each time the loop is executed, the x value is incremented. The loop continues until the value of x is less than 10.
Once the condition is satisfied, the control come out of the loop.
Related posts:
- Shell Scripting Introduction
- Shell Syntax
- Shell - Strings
- Shell - Variables
- Shell - User Intraction
- First Shell Script
- Shell - Arithmetics
- Shell - Case Statement
- Shell - Command Line Arguments
- Shell - For Loop
- Shell - While Loops
- Shell - IF Statements
- Shell - Input Output Redirections
- Shell - Pipes and Filters
- Shell - Process Management
Your IP address1 is: 172.18.0.2
Tutorials
Latest Updates
Follow us
- Tech-Tutorials twitter.com/techcuriosity