I've used the following script (found on this site) and have trouble getting it to work.
askit () {
while true ;
do
read -r -n 1 -p "continue? [y/n] :" rep
case $rep in
[yY] ) return 0 ;;
[nN] ) return 1 ;;
* ) printf "no no, only y or n"
esac
done
}
if $(askit)
then
printf "\nWe go on, %s say.\n" $you
else
printf "\nThat's it, %s want to quit!\n" $you
break
fi
It works for "y" or "n" entries. Other entries loop back without terminal output. After the next "y" or "n" the result is always false.
The newlines in the printf statements are a legacy from not being able to put printf statements within the case statement. I've tried echo as well with the same result.
The return statement seems to be outputting to the if conditional. I'm pretty sure my syntax is ok. I've looked into fflush and stdbuf...
Question: Why does printf output to the if statement and not to the terminal?