I am writing a bash script that should interact (interactively) with an existing (perl) program. Unfortunately, I cannot touch the existing perl program and cannot use expect .
Currently the script runs along the lines of this stackoverflow answer. Is it possible to associate a bash script shell with another command line program?
The problem is that the perl program does not always send <newline>before an input request. This means that bash while ... readin the named pipe does not "receive" (read: display) the output of the perl program because it continues to wait for more. At least, as I understand it.
So, basically the perl program is waiting for input, but the user does not know, because nothing is displayed on the screen.
So what I do in a bash script is roughly
#!/bin/bash
mkfifo $readpipe
mkfifo $writepipe
[call perl program] < $writepipe &> $readpipe &
exec {FDW}>$writepipe
exec {FDR}<$readpipe
...
while IFS= read -r L
do
echo "$L"
done < $readpipe
This works if the perl program does not do something like
print "\n";
print "Choose action:\n";
print "[A]: Action A [B]: Action B\n";
print " [C]: cancel\n";
print " ? ";
print "[C] ";
local $SIG{INT} = 'IGNORE';
$userin = <STDIN> || ''; chomp $userin;
print "\n";
Then the bash script only "sees"
Choose action:
[A]: Action A [B]: Action B
[C]: cancel
but not
? [C]
This is not the most problematic case, but one that is easiest to describe.
Is there a way to make sure that it prints ? [C](I played with cat <$readpipe &, but it really didn't work)?
Or is there a better approach for everyone (given the limitation that I cannot change the perl program, and I cannot use expect)?