0

I would like to know how to do this, if for example, when executing a command in a script, retrieve the message linked to this command. For example "AZERTY already exists, please check the error" is the message that comes out after a command (wrong), how do I so that if my BASH script sees this info, tell it to stop the script?

1
  • Variable="$(echo $message)" Commented Nov 4, 2020 at 16:19

3 Answers 3

3

You almost certainly should not check the error message. Instead just do:

cmd || exit

If cmd fails and writes the message "AZERTY already exists, please check the error" to stderr, then that message will appear and the script will exit with whatever non-zero value cmd exited with. Some commands return non-zero values that have meaning, and you may want to suppress that (for consistent return values from your script) or change that with something like:

cmd || exit 1

On the other hand, if cmd is poorly written and returns a zero value when it "fails" (I'm putting that in quotes since a reasonable definition of "fail" is "return non-zero"), then that is a bug in cmd which should be fixed.

Sign up to request clarification or add additional context in comments.

3 Comments

The command I use is not really fails, she's only result a text : Fileset DBbackup_orintb4h already exists. mmcrfileset: Command failed. Examine previous error messages to determine cause. and just continue to the next command
I'm sorry, but if a command writes an error message which includes the string "Command failed" but does not fail, then you need to redesign the way that command works. Note that echo Command failed is not writing an error message. But echo Command failed >&2 should be re-written as echo Command failed >&2 && false. Conventions like this are extremely important and ought to be honored.
Ok my bad William, it's my fault. I wrote the script (without doing it on purpose) to keep this going. With one modification plus your solution, it works. Thank you very much !
1

I recommend using strict mode in all your bash scripts. http://redsymbol.net/articles/unofficial-bash-strict-mode/

Basically put this at the top

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

The option you specifically asked about is -e

The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful. In all widely used general-purpose programming languages, an unhandled runtime error - whether that's a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.

2 Comments

Note that it is generally considered best practice to avoid set -e since it has many pitfalls and its behavior is different across different shells or different versions of the same shell. It is better to explicitly exit than to rely on set -e
The Wooledge wiki has good articles about set -e and set -u. I personally don't like changing the default for IFS; instead, I use something like ShellCheck to point out all unquoted expansions. All in all, "strict mode" is way too opinionated for me.
0
if ./command | grep -q 'AZERTY already exists, please check the error'; then
  echo "Error found, exiting"
  exit 1
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.