Using and in Bash while loop
The []
operator in bash is syntactic sugar for a call to test
, which is documented in man test
. "or" is expressed by an infix -o
, but you need an "and":
while [ $guess != 5 -a $guess != 10 ]; do
Bash scripting, multiple conditions in while loop
The correct options are (in increasing order of recommendation):
# Single POSIX test command with -o operator (not recommended anymore).
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 -o "$stats" -eq 0 ]
# Two POSIX test commands joined in a list with ||.
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 ] || [ "$stats" -eq 0 ]
# Two bash conditional expressions joined in a list with ||.
while [[ $stats -gt 300 ]] || [[ $stats -eq 0 ]]
# A single bash conditional expression with the || operator.
while [[ $stats -gt 300 || $stats -eq 0 ]]
# Two bash arithmetic expressions joined in a list with ||.
# $ optional, as a string can only be interpreted as a variable
while (( stats > 300 )) || (( stats == 0 ))
# And finally, a single bash arithmetic expression with the || operator.
# $ optional, as a string can only be interpreted as a variable
while (( stats > 300 || stats == 0 ))
Some notes:
Quoting the parameter expansions inside
[[ ... ]]
and((...))
is optional; if the variable is not set,-gt
and-eq
will assume a value of 0.Using
$
is optional inside(( ... ))
, but using it can help avoid unintentional errors. Ifstats
isn't set, then(( stats > 300 ))
will assumestats == 0
, but(( $stats > 300 ))
will produce a syntax error.
Syntax for OR condition in bash while loop
You should use &&
instead of ||
in the condition of while statement.
You are trying to read a name from stdin and if that name is "Jorge" or "Eduardo", you are done. when putting it in condition of while, you want to continue in the loop when the name if not "Jorge" and the name is not "Eduardo".
your current condition says that continue in the loop if the name is not "Jorge" or the name is not "Eduardo". And the name cannot be both at the same time.
How to use while loop in bash
You need an arithmetic statement (two parentheses), not a subshell. let
is unnecessary here.
i=$1
while (( i < 4 )); do
...
done
while
's argument is a shell command. ( i < 4 )
starts a subshell which runs the command i
, reading input from a file named 4
. The redirection is processed before the command is looked up, which explains why you don't get a i: command not found
error.
You can simply replace 4
with the expression you want to use:
while (( i < $1 + $2 )); do
checking two conditions using while loop in bash script
Thanks a lot to all of you... after a good persistence and resilience I finally found the answer of what I was looking for... A posted the code below:
#if the user's input is not Y or N
while [[ $(read -sn1; echo ${character^^}) =~ [^YN] ]];
do
echo -n "Re-enter 'y' to exit or 'n'to continue: ?"
read character
done
In bash script, how to use function exit status in while loop condition
Remove the test
command - also known as [
. So:
while check1
do
# Loop while check1 is successful (returns 0)
if check1
then
echo 'check1 was successful'
fi
done
Shells derived from the Bourne and POSIX shells execute a command after a conditional statement. One way to look at it is that while
and if
test for success or failure, rather than true or false (although true
is considered successful).
By the way, if you must test $?
explicitly (which is not often required) then (in Bash) the (( ))
construct is usually easier to read, as in:
if (( $? == 0 ))
then
echo 'worked'
fi
Related Topics
Does File ".Bash_History" Always Record Every Command I Ever Issue
Is There a Libc in Kernel Space
Why Use G++ Instead of Gcc to Compile *.Cc Files
How to Remount The /Proc Filesystem in a Docker as a R/W System
Yocto Bitbake Script Not Displaying Echo Statement
You Don't Have Permission Error in Apache in Centos
Ssh Connection to Azure Vm with Terraform
"Invalid Arithmetic Operator" in Shell
Do_Install Error While Running Custom Bitbake in Poky Build
How to Install Google Test on Ubuntu Without Root Access
How to Allow Jenkins to Access The Files That Only Root or Some Specific Programs Have Access To
Append to /Etc/Apt/Sources.List
Angular Cli App Not Running When Deploying to Linux App Service
Substituting a String in Place of Variable in Shell
Replace System Call in Linux Kernel 3
Can't Register a Snapshot Repository in Elasticsearch
How to Compile Intel MAC Binaries on Linux
How to Install Devtools Package for R Studio Mounted on Linux Redhat Server