How to capture error message from executed command?

156,345

Solution 1

you can do it by redirecting errors command:

/sbin/modprobe -n -v hfsplus 2> fileName 

as a script

#!/bin/bash
errormessage=$( /sbin/modprobe -n -v hfsplus 2>&1)
echo $errormessage

or

 #!/bin/bash
errormessage=`/sbin/modprobe -n -v hfsplus 2>&1 `
echo $errormessage

if you want to append the error use >> instead of >

Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"

Solution 2

Simply to store as a string in bash script:

X=`/sbin/modprobe -n -v hfsplus 2>&1`
echo $X

This can be a bit better as you will see messages when command is executed:

TMP=$(mktemp)
/sbin/modprobe -n -v hfsplus 2>&1 | tee $TMP
OUTPUT=$(cat $TMP)
echo $OUTPUT
rm $TMP

Solution 3

I capture error like this

. ${file} 2>&1 | {
  read -d "\0" -t 0.01 error
    [ -z "$error" ] || log_warn Load completion ${file} failed: "\n${error}"
}

if source failed, I will capture the error and log it.log_warn is just a simple function.

BTW, I use this in my dotfiles

Solution 4

Newer bash versions (I.e. bash 4.1+):

$ msg=$(ls -la nofile 2>&1)
$ echo $msg
ls: cannot access nofile: No such file or directory
$ 

Solution 5

To return the error message in a variable, simply;

error=$(/sbin/modprobe -n -v hfsplus 2>&1 1>/dev/null)

echo $error
Share:
156,345

Related videos on Youtube

JM S. Tubiera
Author by

JM S. Tubiera

Updated on September 18, 2022

Comments

  • JM S. Tubiera
    JM S. Tubiera over 1 year

    I was tasked to create an automated server hardening script and one thing that they need is a report of all the output of each command executed. I want to store the error message inside a string and append it in a text file.

    Let's say I ran this command:

    /sbin/modprobe -n -v hfsplus
    

    The output of running this in my machine would be:

    FATAL: Module hfsplus not found
    

    How can I store that error message inside a string? Any help would be greatly appreciated. Thanks!

    • JM S. Tubiera
      JM S. Tubiera almost 10 years
      I tried running this command: var=$(/sbin/modprobe -n -v hfsplush) And then displaying it: $var But it still doesn't capture the error message inside the string.
  • JM S. Tubiera
    JM S. Tubiera almost 10 years
    I've tried that approach and it stores it DIRECTLY in the text file. I want it to store inside a string first so I can format the contents easily.
  • JM S. Tubiera
    JM S. Tubiera almost 10 years
    I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker!
  • Sreeraj
    Sreeraj over 9 years
    Always use $(command) instead of backticks for command substitution. It is better :)
  • peter_v
    peter_v almost 6 years
    I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'"
  • KolonUK
    KolonUK over 4 years
    I know it is better (I've had less issues using $()), but why is that?
  • Nobody
    Nobody almost 4 years
    @KolonUK For one thing, you can nest them.
  • R. W. Prado
    R. W. Prado over 2 years
    That's the answer, since the other ones would append standard output to the variable. Usually, you want to know if the message comes from a error or not and that's perfect. For example, to throw a warning or to handle a bad situation in a script.