Mark Lucernas
Aug 09, 2020

Output Streams

Redirecting Standard Output to a File

Redirect stdout replacing existing file*

  • command 1> /path/to/file
  • command > /path/to/file1 can be omitted

Redirect stdout appending the output if file exists, else create new

  • command >> /path/to/file

Print output normally and redirect to a file

  • command | tee /path/to/file

Print output normally and redirect to a file appending if existing, else create new

  • command | tee -a /path/to/file

Ref:

Redirecting Standard Output and Standard Error to a File

Redirect stdout to one file and stderr to another file

  • command > out 2>error

NOTE: out and error are the path to files

Redirect stdout to a file > file, and then redirect stderr to stdout 2>&1

  • command > file 2>&1
  • command > /dev/null 2>&1 – redirect to /dev/null to silence output

Redirect both stdout and stderr to a file (shorthand)

  • command &> file
  • command &> /dev/null – redirect to /dev/null to silence output

Redirect stdout and stderr to a bash function

Method 1:

LogMsg()
{
  read IN # This reads a string from stdin and stores it in a variable called IN
  DateTime=`date "+%Y/%m/%d %H:%M:%S"`
  echo '*****'$DateTime' ('$QMAKESPEC'): '$IN >> "$LogFile"
  echo $DateTime' ('$QMAKESPEC'): '$IN
}

# Use with pipe
make 2>&1 | LogMsg

Method 2:

LogMsg()
{
  if [ -n "$1" ]
  then
      IN="$1"
  else
      read IN # This reads a string from stdin and stores it in a variable called IN
  fi

  DateTime=`date "+%Y/%m/%d %H:%M:%S"`
  echo '*****'$DateTime' ('$QMAKESPEC'): '$IN >> "$LogFile"
  echo $DateTime' ('$QMAKESPEC'): '$IN
}

# Use with pipe
make 2>&1 | LogMsg

# Use without pipe
LogMsg "Message"

NOTE: This makes LogMsg take in an argument if not used with pipe

Ref:


Resources