while loop bash
while true;
do
#code
done
4
4
#!/bin/sh
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
Thank you!
4
0
0
0
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Thank you!
0
0
4.33
9
while true;
do
#code
;done
Thank you!
9
0
0
0
while CONDITION_STATEMENT; do SOME_CODE; done
Thank you!
0
0
4.6
5
#!/bin/sh
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
Thank you!
5
0
4.8
5
# The syntax is as follows:
while [ condition ]
do
command1
command2
command3
done
# command1 to command3 will be executed repeatedly till the 'condition'
# is true.
# The argument for a while loop can be any boolean expression.
# Infinite loop occurs when the conditional never evaluates to false.
# Here is the while loop for a one-liner syntax:
while [ condition ]; do commands; done
while control-command; do COMMANDS; done
# For example, the following while loop will print 'welcome x times' 5 times
# on the screen:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
# as one-liner:
x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done
# Here is a sample shell code to calculate factorial using while loop:
#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
# To run just type:
$ chmod +x script.sh
$ ./script.sh 5
Thank you!
5
0
4
8
while true;
do
#code
;done
Thank you!
8
0
4.67
3
while CONDITION_STATEMENT; do SOME_CODE; done
Thank you!
3
0
4
7
# The syntax is as follows:
while [ condition ]
do
command1
command2
command3
done
# command1 to command3 will be executed repeatedly till the 'condition'
# is true.
# The argument for a while loop can be any boolean expression.
# Infinite loop occurs when the conditional never evaluates to false.
# Here is the while loop for a one-liner syntax:
while [ condition ]; do commands; done
while control-command; do COMMANDS; done
# For example, the following while loop will print 'welcome x times' 5 times
# on the screen:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
# as one-liner:
x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done
# Here is a sample shell code to calculate factorial using while loop:
#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
# To run just type:
$ chmod +x script.sh
$ ./script.sh 5
Thank you!
7
0
Are there any code examples left?
New code examples in category Shell/Bash
-
Shell/Bash 2022-03-27 22:30:33 wise highlight ubuntu
-
Shell/Bash 2022-03-27 22:25:19 how to send a pull request in git
-
Shell/Bash 2022-03-27 21:55:06 how to open terminal in sublime text ubuntu
-
Shell/Bash 2022-03-27 21:25:19 how to switch branch
-
Shell/Bash 2022-03-27 21:20:25 python
-
Shell/Bash 2022-03-27 21:20:04 set user as admin gitlab
-
Shell/Bash 2022-03-27 21:10:09 apache2 mod_jk proxy to tomcat connector
-
Shell/Bash 2022-03-27 20:35:12 chown
-
Shell/Bash 2022-03-27 20:15:08 awk last match