Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash scripting, multiple conditions in while loop

I'm trying to get a simple while loop working in bash that uses two conditions, but after trying many different syntax from various forums, I can't stop throwing an error. Here is what I have:

while [ $stats -gt 300 ] -o [ $stats -eq 0 ] 

I have also tried:

while [[ $stats -gt 300 ] || [ $stats -eq 0 ]] 

... as well as several others constructs. I want this loop to continue while $stats is > 300 or if $stats = 0.

like image 590
jake9115 Avatar asked Mar 20 '13 20:03

jake9115


People also ask

Can you have multiple conditions in a while loop?

Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator. By using if statements and logical operators such as &&, 11,!

What is && and || in shell script?

Logical AND operator(&&): The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.

Can you use && in bash?

The Bash logical (&&) operator is one of the most useful commands that can be used in multiple ways, like you can use in the conditional statement or execute multiple commands simultaneously.

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

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.

like image 134
chepner Avatar answered Sep 19 '22 11:09

chepner