jimx0r: (Default)
[personal profile] jimx0r
So, what I'd really really like is to be able to run a command and dump the output of the command to a logfile. I *need* to be able to check on the return status of the command (so a simple cmd.exe | tee -a $LOGFILE will not work as the return value is that of the tee command and not the cmd.exe!) so that I can do sensible things inside the rest of my shell script.

So far, I've come up with this bit of code that runs the command, saves the output of the command to the variable CMDOUT, saves the return value of the command to RET, barfs the output of the command into the logfile (and to STDOUT) and then returns the same return value as the command.

# write stuff to a log file
# Call like this: log_it "command1 | command2 ; command3 & command4" or whatever set of shell commands you'd like
function log_it () {
  cmds=$@
  RET=42 
  TMPFILE=$(mktemp)
  CMDOUT=$(/bin/bash -c "$cmds" 2>&1)
  RET=$?
  echo "$CMDOUT" | tee -a $LOGFILE
  return $RET
}
What I *really really hate* about the above code is that I cannot see the output of the command until the command has completed.  I'd really like the behavior to be more like the naive implementation using tee that I alluded to above in that I see the output as it happens... except, of course, that I *need* that return code!

Re: Nice Hack!

Date: 2012-07-31 06:08 pm (UTC)
vatine: Generated with some CL code and a hand-designed blackletter font (Default)
From: [personal profile] vatine
It's relatively simple in C...

Profile

jimx0r: (Default)
jimx0r

July 2012

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930 31    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 15th, 2025 10:04 am
Powered by Dreamwidth Studios