Say Yes? Say No? Say what?

Anyone who has written an interactive script has had to ask a yes or no question. And then script gets changed to handle UPPERCASE and lowercase. And modified again to check for just 1 letter such as y or n… And pretty soon, the code looks fairly convoluted. Here’s a simple way to handle all 3 conditions (yesno and other):

# Ask question:

echo “Is this correct (y/n)? c”

read ANSWER
case ANSWER in

[Yy] | [Yy][Ee][Ss] )    echo “ANS is yes”

                ;;

[Nn] | [Nn][Oo] )     echo “Ans is no”

                ;;

            * )    echo “ANS is not yes or no”

                ;;

esac

So the code recognizes single letters y/n or Y/N, as well as the full word.

Now, the opposite of asking the yes or no question is to supply the answer. What? How do you automatically answer a question? The answer is yes. That’s right. yes is the answer. Check out: man yes This is a program to endlessly supply “y” or any string you need to give to a program that runs in cron or batch mode and there is no one to answer the question. The yes program runs forever (ctrl-c to terminate):

yes | head -3

y

y

y

yes no | head -3

no

no

no

An example for batch usage is fbackup. If fbackup encounters an error condition, it will hang forever waiting for the answer (do you want to continue, do you want to save…). Since the backup is unattended, answering no is very useful. So here is the technique:

yes no | fbackup -f /dev/rmt/0mm -i /

In this case, when fbackup encounters an error, all questions will be answered with “no“.

– See more at: http://serviceitdirect.com/blog/say-yes-say-no-say-what#sthash.ekfsHO9E.dpuf


Tags: