Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to do multiple statements per bash test && statement

Tags:

Does anyone know of a way to execute multiple statements within a bash test? So if I use:

[[ $Var = 1 ]] && echo "yes-1" || echo "no-1" 

And set Var=1 then output is: yes-1
If i set Var=2 then output is: no-1

And this work as I expected. But If i try to add another statement to execute in the mix and it doesn't work:

[[ $Var = 1 ]] && echo "yes-1";echo "yes-2" || echo "no-1";echo "no-2" 

Which makes sense as bash sees the command ending at; but... this is not what I want.

I've tried grouping and evals and functions and have had failures and successes but I'd really just like to do is have this work on one line. Anyone have any ideas?

like image 526
pn1 dude Avatar asked Jun 14 '12 17:06

pn1 dude


People also ask

How do you run multiple commands in one line?

Running Multiple Commands as a Single Job We can start multiple commands as a single job through three steps: Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.

Can bash commands be combined?

Group Commands. In Bash, we can use both “{ }” and “( )” operators to group commands.

What is $@ in bash?

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.


2 Answers

Simple command grouping should work; the syntax can be a little tricky though.

[[ $Var = 1 ]] && { echo "yes-1"; echo "yes-2"; } || { echo "no-1"; echo "no-2"; } 

A few things to note:

  1. Heed @tvm's advice about using an if-then-else statement if you do anything more complicated.

  2. Every command inside the braces needs to be terminated with a semi-colon, even the last one.

  3. Each brace must be separated from the surrounding text by spaces on both sides. Braces don't cause word breaks in bash, so "{echo" is a single word, "{ echo" is a brace followed by the word "echo".

like image 50
chepner Avatar answered Oct 13 '22 01:10

chepner


Consider using regular IF THEN ELSE statement. Use of && and || is justified in simple test such as this:

[[ -z "$PATH" ]] && echo 'Disaster, PATH is empty!' || echo 'Everything ok!' 

But, consider following command:

true && true && true && false && true || echo 'False!' False! 

OR

true && { echo true; false ; } || { echo false; true ; } true false 

Anytime a non-zero exit status is returned, command after || is executed. As you can see, even command grouping doesn't help.

Execution in subshell behaves in similar manner:

true && ( true; echo true; true; false ) || ( true; echo true; false ) true true 

Just use regular IF, if you need proper IF behavior.

like image 36
tvm Avatar answered Oct 12 '22 23:10

tvm