Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Is it possible to capture stdout and stderr to separate variables in Zsh?



On Mar 7, 11:37am, Philippe Troin wrote:
}
} > }         coproc cat &
} > }         pid=$!
} > }         stdout="$( ( print "printed on stdout"; print -u 2 "printer on stderr" ) 2>&p )"
} > }         sleep 1 
} > }         kill "$pid"
} > }         stderr="$(cat <&p)"
} > 
} > You need this:  http://www.zsh.org/mla/users/2011/msg00095.html  :-)
} 
} I didn't see anything in there that could suppress the sleep+kill.

Look at the part subtitled "Where might I go wrong with coproc?" ... it
doesn't answer the whole problem, but it tells you how to cleanly close
the descriptors.  The full solution is more like:

	coproc cat &
	exec {p}<&p		# Copy the <&p descriptor
	stdout="$( ( print "printed on stdout";
	             print -u 2 "printed on stderr") 2>&p )"
	coproc exit		# Close both <&p and >&p descriptors
	stderr="$(cat <&$p)"	# Read from copy descriptor
	exec {p}<&-		# Close the copy (optional)

I also dug this up:  http://www.zsh.org/mla/workers/2000/msg03684.html
but that predates the fancy {p}<&p syntax.

} I like Nikolai's solution best, except that it's somewhat cryptic

Yeah, I may have some thoughts on that one, too.



Messages sorted by: Reverse Date, Date, Thread, Author