You're right.
read(0) is reading from the file, or more precisely from the file descriptor 0 which is redirected to the file called
myfile.
So you have to specify that you want to read the user's input from the
stan
dard
input, which is the file descriptor 0. ( «
help '\read' » )
I've done an improved version... redirecting FD 3 to the file to avoid redirecting FD 0.
In this case, it's better, because you don't know whether the file contains many lines.
Code:
#!/bin/bash
if (($# != 1)); then
printf 'usage: ./%s filename\n' "${0##*/}" >&2
exit 1
elif [[ ! -f $1 ]]; then
printf '« %s »: no such file found.\n' "$1" >&2
exit 1
fi
exec 3< "$1"
shopt -s extglob nocasematch
echo $'Is it the correct line?\n'
_line_found=0
while ((! _line_found)) && IFS= read -r -u 3; do
if [[ ! ${REPLY//[[:space:]]} ]]; then
continue
fi
if ! read -er -i nope -p "« $REPLY » ? " \
-t 10 ans; then
echo
fi
if [[ $ans = @(q|quit|exit) ]]; then
exit
elif [[ $ans = y || $ans = ye[sp] ]]; then
echo $'\nLine found!'
_line_found=1
fi
done
exec 3>&-