Operating System - HP-UX
1753980 Members
6687 Online
108811 Solutions
New Discussion юеВ

Re: How do I ask a question in ksh and proceed accordingly?

 
Stuart Abramson
Trusted Contributor

How do I ask a question in ksh and proceed accordingly?

I want to write a ksh script which asks the user something like:

"Do you want to continue?"

and if answers "yes", then continue.

I'm just drawing a blank here. How do I do that:
print "question"
read "answer"
check response? Will the read wait for the answer? Do I have to do something special? Can I time out the read response if he does't answer in 5 minutes?
5 REPLIES 5
Slawomir Gora
Honored Contributor

Re: How do I ask a question in ksh and proceed accordingly?

Hi,
simple script, you can use "if" instead of case

#!/usr/bin/ksh

print "Do you want to continue?"
read ans
case "$ans" in
"yes")
print "Answer is yes"
;;
"no")
print "Answer is no"
;;
*)
print "Wrong answer"
;;
esac
Pete Randall
Outstanding Contributor

Re: How do I ask a question in ksh and proceed accordingly?

Stuart,

Standard technique is something like:

echo "question"
read "answer"

(test answer)

I suppose you could test the answer for null, sleep 60, retest, something like that.


Pete

Pete
Stuart Abramson
Trusted Contributor

Re: How do I ask a question in ksh and proceed accordingly?

I found it. Sorry for bother.

##
# Script to read an answer to a question..
#
read ANSWER?"Do you want to vgexport live VGs (yes/no): "
typeset -l LOW_ANSWER=$ANSWER

if [[ $LOW_ANSWER != "yes" ]]
then
print "You didn't enter 'yes'. Script will exit without vgexport."
print "If you want to vgexport VGs, run script again and answer 'yes'."
exit 1
fi

# Answer was yes. vgexport

print "You answered 'yes'. vgexports will run right now."

print "vgexport successfully run!"
Bill Hassell
Honored Contributor

Re: How do I ask a question in ksh and proceed accordingly?

You can also do more with the case statement to handle no input (just a return) and a default value:

# Script to read an answer to a question..
#
typeset -l ANSWER
read ANSWER?"Do you want to vgexport live VGs (yes/[no]): "

case $ANSWER in
y* ) print "You answered 'yes'. vgexports will run right now."
#...insert your vgexport code...
print "vgexport successfully run!"
;;
* ) print "You didn't enter 'yes'. Script will exit without vgexport."
print "If you want to vgexport VGs, run script again and answer 'yes'."
exit 1
;;
esac

The pattern matching in case allows for simpler tests for various answers: y ye yes yessir! and so on.


Bill Hassell, sysadmin
Stuart Abramson
Trusted Contributor

Re: How do I ask a question in ksh and proceed accordingly?

Thanks all...