2

I'm having a Bash-Script that sequentially runs some Perl-Scripts which are read from a file. These scripts require the press of Enter to continue.

Strangely when I run the script it's never waiting for the input but just continues. I assume something in the Bash-Script is interpreted as an Enter or some other Key-Press and makes the Perl continue.

I'm sure there is a solution out there but don't really know what to look for.

My Bash has this while-Loop which iterates through the list of Perl-Scripts (which is listed in seqfile)

while read zeile; do
    if [[ ${datei:0:1} -ne 'p' ]]; then
        datei=${zeile:8}
    else
        datei=$zeile
    fi
    case ${zeile: -3} in
    ".pl")
    perl  $datei  #Here it just goes on...
    #echo "Test 1"
    #echo "Test 2"
    ;;
    ".pm")
        echo $datei "is a Perl Module" 
    ;;
    *)
        echo "Something elso"
    ;;
    esac
done <<< $seqfile;

You notice the two commented lines With echo "Test 1/2". I wanted to know how they are displayed. Actually they are written under each other like there was an Enter-Press:

Test 1
Test 2

The output of the Perl-Scripts is correct I just have to figure out a way how to force the input to be read from the user and not from the script.

3
  • what happens is perl script is executed on command line. Does it return the prompt immediately? Commented Feb 7, 2018 at 17:57
  • Print the value of $datei to make sure is what you want. Commented Feb 7, 2018 at 18:12
  • @LuisMuñoz - if the script is executed on the command line it works as it's supposed to be (after executing various Perl-Scripts several times with the same command I decided to do some automation). Actually, the Bash-Script works - it just jumps over the input I want to give. Commented Feb 7, 2018 at 18:15

2 Answers 2

3

Have the perl script redirect input from /dev/tty.

Proof of concept:

while read line ; do
    export line
    perl -e 'print "Enter $ENV{line}: ";$y=<STDIN>;print "$ENV{line} is $y\n"' </dev/tty
done <<EOF
foo
bar
EOF

Program output (user input in bold):

Enter foo: 123 foo is 123
Enter bar: 456 bar is 456

Sign up to request clarification or add additional context in comments.

2 Comments

I had to re-write it a little, but basically just change the one line to perl $zeile </dev/tty . I don't want it to wait for every single input (sometimes no input is required at all). I see your knowledge of bash is bigger than mine, would you mind explaining also why you use the -e Parameter and export the line before instead of just using it directly? (Upvoted and Marked as answer already, but some explanation would be nice, thank you)
The -e and the export probably aren't relevant to your specific problem. -e instructs perl to treat the next command line argument as perl code to execute rather than a file containing Perl code. export makes the bash variable $line available in the Perl process so I could output it.
3

@mob's answer is interesting, but I'd like to propose an alternative solution for your use case that will also work if your overall bash script is run with a specific input redirection (i.e. not /dev/tty).

Minimal working example:

  • script.perl

    #!/usr/bin/env perl
    use strict;
    use warnings;
    {
        local( $| ) = ( 1 );
        print "Press ENTER to continue: ";
        my $resp = <STDIN>;
    }
    print "OK\n";
    
  • script.bash

    #!/bin/bash
    
    exec 3>&0 # backup STDIN to fd 3
    
    while read line; do
        echo "$line"
        perl "script.perl" <&3 # redirect fd 3 to perl's input
    done <<EOF
    First
    Second
    EOF
    
    exec 3>&- # close fd 3
    

So this will work with both: ./script.bash in a terminal and yes | ./script.bash for example...

For more info on redirections, see e.g. this article or this cheat sheet.

Hoping this helps

4 Comments

You were just 8 seconds too late I'm afraid but I find your answer interesting and your links helpful (upvote!), yet I can't make your solution work :/ bash: [[: /: syntax error: operand expected (error token is "/") bash: 3: Bad file descriptor
@Qohelet I cannot reproduce your error. Did you copy the script.bash file as is, and run chmod a+x script.bash && ./script.bash ? If yes, can you give more hints about your config (such as the OS and the shell)? Do you have the same error if you run exec 3>&0 in your terminal?
You may also want to run ls -l /dev/fd/ in a terminal to get the list of opened file descriptors
Overlooked your exec 3>&0 and didn't put it in my code :/ - your idea works perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.