Here document – copy stdin to stdout?

I would like to do auto testing one of my scripts and here documents are almost perfect. It would be nice however to be able to copy the STDIN to STDOUT so it’s just as if it was typed in when the here document is pushed into the script (see the inputs on every run). Is this easily doable?

I have at the moment (doesn’t show input as user would type and see, only shows output):

#!/bin/sh

make && ./proj <<- EOF
i
3
i
9
i
55
i
345
t
s
33
s
455
i
44
i
99
t
q
EOF

Answer

You could duplicate it with tee, and then use fd3 to sneak it around your script with something like this:

{ make && tee /dev/fd/3 <<EOF | ./proj
1
3
i
9
EOF
} 3>&1

…but I wouldn’t trust anything other than the version of bash I tested this on to parse it the same way. Darth’s suggestion of using a temp file would work, or if there’s not too much input you could do essentially the same thing with a variable:

input='1
3
i
9'
make && echo "$input" && echo "$input" | ./proj

…although this also may break on a few shells that can’t cope with linebreaks in variable values.

Attribution
Source : Link , Question Author : Joshua Enfield , Answer Author : Gordon Davisson

Leave a Comment