0

i am using read -p "Press any key to continue" in my script. This works fine except when it is in a while loop like

while read TEST_NAME  ; do
    read -p "Press any key..."
    echo "Executing:"
done <$MT_TEST_ROOT_DIR/automation.mts

i am suspecting it is because of the enclosing while looping doing the read as well. so what would be the solution for it

Vik
  • 7,983
  • 25
  • 77
  • 154

2 Answers2

1

The issue is indeed the enclosing redirection. You can simply redirect the inner read input from /dev/tty (your keyboard) that way:

while read TEST_NAME  ; do
    read -p "Press any key..." < /dev/tty
    echo "Executing:"
done <$MT_TEST_ROOT_DIR/automation.mts
jlliagre
  • 28,380
  • 6
  • 58
  • 69
0

Both reads are reading from the same input file. Use a different file descriptor for the first read, letting the second read inherit whatever your script is using.

while read TEST_NAME <&3  ; do
    read -p "Press any key..."
    echo "Executing:"
done 3< "$MT_TEST_ROOT_DIR"/automation.mts
chepner
  • 446,329
  • 63
  • 468
  • 610