0

I'm looking for an opposite of this: Trick an application into thinking its stdin is interactive, not a pipe

I'd like to get the output of a command on stdout, but make it think it's writing into a pipe.

The usual solution is to | cat but I have the additional requirement that this is cross platform (ie sh, not bash) and returns a valid exit code if the command fails. Normally I would use pipefail but this isn't available everywhere.

I've tried various incantations of stty but haven't been successful.

Community
  • 1
  • 1
AlexH
  • 87
  • 1
  • 3

1 Answers1

1

You could always use a named pipe:

mkfifo tmp.pipe

# Reader runs in background
cat tmp.pipe &

# Producer in foreground
your_command > tmp.pipe
command_rtn=$?

rm tmp.pipe

Alternately, if you don't need the output in realtime and the output is reasonably small:

output=$(your_command)
command_rtn=$?
echo "${output}"

Or you can write the exit status to a file doing something terrible like:

{ your_command; echo $? > rtn_file; } | cat
command_rtn=$(cat rtn_file)
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115