Using "And" in Bash While Loop

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:

  1. Quoting the parameter expansions inside [[ ... ]] and ((...)) is optional; if the variable is not set, -gt and -eq will assume a value of 0.

  2. Using $ is optional inside (( ... )), but using it can help avoid unintentional errors. If stats isn't set, then (( stats > 300 )) will assume stats == 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



Leave a reply



Submit