0

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
1

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.

1

You want cat.

1

You can also use redirection. Save your test input into test.txt, and then make && ./proj < test.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.