shell programming
Created:
Updated:
while loop in shell file
while_loop.sh
1
2
3
4
5
6
7
8
| #!/bin/bash
num=0
while [ $num -le 5 ]
do
echo "num : ${num}"
((num++))
done
|
output
1
2
3
4
5
6
7
| $ ./while_loop.sh
num : 0
num : 1
num : 2
num : 3
num : 4
num : 5
|
while loop in single-line
To do this, use the end-of-line delimiter(;).
example
1
| $ num=0; while [ $num -le 5 ]; do echo "num : ${num}"; ((num++)); done
|
output
1
2
3
4
5
6
7
| $ num=0; while [ $num -le 5 ]; do echo "num : ${num}"; ((num++)); done
num : 0
num : 1
num : 2
num : 3
num : 4
num : 5
|
infinite while loop
example
1
| $ num=0; while ((1)); do echo "num : ${num}"; ((num++)); sleep 1; done
|
output
1
2
3
4
5
6
7
8
9
| $ num=0; while ((1)); do echo "num : ${num}"; ((num++)); sleep 1; done
num : 0
num : 1
num : 2
num : 3
num : 4
num : 5
...
...
|
Leave a comment