Bash question - redirect stderr to a variable

Bryan O'Neal Bryan.ONeal at TheONealAndAssociates.com
Mon Mar 14 22:45:32 MST 2011


Ok - if I want to direct stranded out, standard error, or both to a
file I can do that. If I want to redirect standard out to a variable
for latter use, I can do that. But how do I redirect standard error to
a variable?

Example:

function fun1()
{
    val1=$(DoSomething with $1)
    if [[ $? -eq 1 ]]; then
       echo "DoSomething failed on value $1" >> logfile.log
       return 1
    else
       if [[ $val1 some test ]]; then
          echo "Condition met" >> logfile.log
          val2=$(DoSomethingElse with something and $val1 and $1)
          echo $val2
          return 0
       else
          echo "Condition not met" >> logfile.log
          return 1
      fi
   fi
}

fun1 somevalue



So this is fine and good - but what if I want to know what the error
was from a bad call to DoSomething - Further more what if instead of
using a simple >> logfile.log I am using a complex function that
correlated messages and creates a human readable log file? I would 1)
spam roots email, and 2) lose the information as to what went wrong
and where?  I want to store std error into a variable (same or
diffident var from expected output) so I can choose how to handle it.


On of the things I have done is started to work on a logger4Bash
function. It took a few min and has deferent log levels, deferent log
files, tags, formatting, etc. Problem is what to do when information
is spawned to standard out instead of being stored in a var I can
manipulate latter.


More information about the PLUG-discuss mailing list