1753971 Members
8189 Online
108811 Solutions
New Discussion юеВ

question on scripting

 
SOLVED
Go to solution
Mark Harshman_1
Regular Advisor

question on scripting

i'm trying to issue a prompt in a script, that will require a y/n response before contining. SO far, no luck. ANy help would be appreciated. thanks
Never underestimate the power of stupid people in large groups
12 REPLIES 12
Pete Randall
Outstanding Contributor

Re: question on scripting

You need to use "read":

echo "enter yes or no"
read ANSWER

You can then test the variable for "yes" or "no".


Pete

Pete
Shahul
Esteemed Contributor

Re: question on scripting

Hi,

This will do what you are looking for.

read answer
if [ $answer = "Y" ]
then
do this
else
do this
fi

You can also use case statment.

Hope this helps.

Shahul
James R. Ferguson
Acclaimed Contributor

Re: question on scripting

Hi Mark:

#/usr/bin/sh
echo "Say something"
read REPLY
echo "You said ${REPLY} ...thank you!"
exit 0

Regards!

...JRF...
Mark Harshman_1
Regular Advisor

Re: question on scripting

the echo statement prints the message, but doesnt pause for a reply, what am i missing? thanks
Never underestimate the power of stupid people in large groups
James R. Ferguson
Acclaimed Contributor
Solution

Re: question on scripting

Hi Mark:

The 'read' waits for input.

#!/usr/bin/sh
typeset -l REPLY
echo "Answer 'y' or 'n' \c"
read REPLY

while [ "${REPLY}" != "y" -a "${REPLY}" != "n" ]
do
echo "Please answer 'y' or 'n'"
read REPLY
done
echo "You replied '${REPLY}' -- thanks!"
exit 0

...In the above script we keep asking for a REPLY until the user responds with a "y" or an "n". The 'typeset -l' causes the contents of the REPLY variable which is filled by the 'read' to be translated to lowercase letters. This makes the comparisons for a proper response easy.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: question on scripting

The read command should pause and wait form input; however, a few things could make it not do this:

1) You have redirected stdin.
2) There are already characters in the input buffer so the reply is being satisfied from the buffer.
3) No read permission on the stdin device.
4) You have changed the terminal characterics so that it times out if no characters are available.

If it ain't broke, I can fix that.
Mark Harshman_1
Regular Advisor

Re: question on scripting

ok, i am trying to use this within a read-while-do setup. I am trying basically reading a file, and asking the user if they want to proceed with the entry printed. Doing the "echo" and "read" within this does not seem to work. is there a way i can still do this within my existing read-while-do? am i confusing the issue? thanks
Never underestimate the power of stupid people in large groups
James R. Ferguson
Acclaimed Contributor

Re: question on scripting

Hi Mark:

If you are issuing a 'read' to collect a user's input and are already reading a file, you will need to use *separate* file descriptors to keep the data segregated.

#!/usr/bin/sh
exec 3<&0
cat /etc/hosts | while read LINE
do
echo "${LINE}"
read -u3 REPLY
echo "You said ${REPLY}"
done

If the above example, I'm reading LINEs from the '/etc/hosts file; echoing them to STDOUT; and *read*ing from STDIN into the REPLY variable. The 'read -u' variation says that I want that read to be from file descriptor #3 which is a duplicate of descriptor zero (0) [STDIN] the beginning of the script.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: question on scripting

It sounds like you redirect input with the while loop. You can force read to read from another device then STDIN.

read REPLY <&2

This will force read to read from STDERR which should still be assigned to your terminal.

Or if STDERR is redirected, then you can use-

read REPLY
HTH

-- Rod Hills
There be dragons...