1

I know this has probably been asked in various ways on this forum, and around the web, but I have tried all methods to no avail. I have a while read loop with a "read -p" contained in an if statement within it. I understand that the standard input is the .txt file being pumped into the while loop, so read-p can't be piped to user input. What will be my recourse here. Please take a look at the code.

while read orasid1
do
if [ "$prompt1" = "yes" ]; then
read -p "Do you want to change the password for $username on $orasid (y/n)?" Yesno2

    case ${Yesno2:0:1} in
            y|Y )

    echo "Yes"
#Call SQL script here
    ;;
    * )
    "No"
    ;;
    esac

else

exit 16

fi

done < $log_dir/pwd_change_$server.txt

I'll appreciate whatever help anyone can give.

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
Lucasetor
  • 13
  • 3
  • Don't use standard input for the outer loop. Use an alternate fd instead. Or open an fd to the tty and use that fd in the inner read. – Etan Reisner Jun 12 '15 at 19:24

1 Answers1

2

Use a different file handle for the outer read:

while read -u 3 orasid1; do
    ...
done 3< "$log_dir/pwd_change_$server.txt"

This is useful for any command (not just read) inside the body of the loop which might read (necessarily or not) from standard input.

You are still free to redirect standard input for the script as a whole from another file, for instance if you want to store canned responses to the various prompts in a file.

chepner
  • 497,756
  • 71
  • 530
  • 681