[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash: pipe once more

> cd does not seem to do its thing when stdout is redirected to a pipe, 
> however it does work with a (temporary) file:
> cd bad > cd.out 2>&1
> cat cd.out
> rm cd.out

I ultimately came up with this variant:



function handle {
  if [ -f $TEMP_LOG ]
    cat $TEMP_LOG | tee -a $LOG_FILE
  exit 1

function clean {
  rm -f $TEMP_LOG

trap handle ERR
trap clean EXIT

# do some stuff

cd nonexistent > $TEMP_LOG 2>&1

# do some more stuff

It might seem like an overkill, but since I'm using the error handling in my script anyway it's actually a nice way to keep the noise (cat & rm) out of my main function. Of course this variant only logs the errors of a cd call, but since cd usually doesn't produce any meaningful output to stdout this seems quite okay to me. If you were to use a command which changes the state of the current shell and provides meaningful output to stdout and you'd want to pipe it, you'd just have to use the above three-liner consecutively or come up with some other variant.


Reply to: