16

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!

  • 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. – Miguel Roque May 29 '14 at 7:42
19

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 `&'"

  • 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. – Miguel Roque May 29 '14 at 7:45
  • 1
    @MiguelRoque see updates – Networker May 29 '14 at 7:46
  • 1
    I tried putting the output inside a HEREDOC and it worked also. Thanks a lot @Networker! – Miguel Roque May 29 '14 at 7:52
  • 1
    Someone reverted the edit I did, because I had a "syntax error near &" and removed the space after >. A justification would have been nice. – Pierre.Sassoulas Jan 12 '18 at 10:08
  • I tried to edit because : Make sure to use 2>&1 and not 2> &1 to avoid the error "syntax error near unexpected token `&'" – peter_v Jun 28 '18 at 8:14
14

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
  • 1
    Always use $(command) instead of backticks for command substitution. It is better :) – Sree Feb 13 '15 at 7:02
4

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

2

To append to a file use /sbin/modprobe -n -v hfsplus 2>> filename

2

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
$ 
0

To return the error message in a variable, simply;

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

echo $error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.