Execute two commands if the first one fails

AGamePlayer

I am writing a script.sh like:

ls a || ls > /dev/null || echo "both command fails"; exit 3
echo 5 # not showed up

I hope the exit 3 be only executed when both command1 and command2 failed. But it seems that even if ls > /dev/null is ok, the exit 3 still fired thus makes echo 5 never showed up.

Anything I can do to fix this?

Thomas Erker

The problem is that || binds tighter than ;, so your first line consists of two commands: the first is a compound command: ls || ls || echo and the second is just exit 3. As mentioned in a comment by meuh, you should write

ls a || ls > /dev/null || { echo "both command fails"; exit 3; }
echo 5 # not showed up

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash - Execute two commands and get exit status 1 if first fails

From Dev

Execute multiple check's even if the first one fails

From Dev

Execute two commands in a row using one SSH object via paramiko

From Dev

Can I execute two commands in one line in batch?

From Dev

Chef execute fails for the first time

From Dev

batch: Execute two commands in sequence

From Dev

How to execute two commands sequentially

From Dev

Execute a line of commands with one sudo

From Dev

Two commands to one button

From Dev

Two commands in one

From Dev

Two commands, one pipeline

From Dev

Two commands in one line

From Dev

Multiple commands in one line fails

From Dev

Salt fails to execute commands with pipes or redirects

From Dev

Why when I have two mysqli queries one after another I can execute only the first one?

From Dev

Combine two commands into one command

From Dev

Executing two commands in one line

From Dev

Combine two commands into one command

From Dev

Combine two awk commands into one

From Dev

Two commands in one batch file?

From Dev

bind two commands for one button

From Dev

Combine two find commands in one

From Dev

How to execute two grep commands on the same file?

From Dev

Unable to execute two commands in Startup Applications

From Dev

pass multiple commands for cmd to execute as one block

From Java

Execute combine multiple Linux commands in one line

From Dev

Laravel 5 Commands - Execute one after other

From Dev

Laravel 5 Commands - Execute one after other

From Dev

Is there a way to search through the eclipse commands to execute one?