I currently have a Perl script (that I can't edit) that asks the user a few questions, and then prints a generated output to stdout. I want to make another script that calls this script, allows the user to interact with it as normal, and then stores the output from stdout to a variable.
Here's a really simple example of what I'm trying to accomplish:
inner.pl
#!/usr/bin/perl
print "Enter a number:";
$reply = <>;
print "$reply";
outer.sh (based on the answer by Op De Cirkel here)
#!/bin/bash
echo "Calling inner.pl"
exec 5>&1
OUTPUT=$(./inner.pl | tee >(cat - >&5))
echo "Retrieved output: $OUTPUT"
Desired output:
$ ./outer.sh
Calling inner.pl
Enter a number: 7
7
Retrieved output: 7
However, when I try this, the script will output Calling inner.pl
before "hanging" without printing anything from inner.sh.
I've found a bit of a workaround by using the script
command to store the entire inner.sh exchange to a temporary file, and then using sed
and the like to modify it to my needs. But making temporary files for something fairly trivial like that doesn't make a ton of sense (not to mention script
likes to add time stamps and \r
s to everything). Is there any non-script
way to accomplish this?