Operating System - HP-UX
1834394 Members
1701 Online
110066 Solutions
New Discussion

Posix shell scripting... prompting for a response from within a "while read" loop

 
SOLVED
Go to solution
Kurt Renner
Frequent Advisor

Posix shell scripting... prompting for a response from within a "while read" loop

Consider the following code:

find / ! -fstype nfs -only -perm -o+w -type d | while read line; do
echo "Do you want to change permissions on file ${line## *}."
read answer
done

I understand why this doesn't give me the results I am looking for. The line "read answer" is loading the answer variable with the next line from the "find... " command.

How can I prompt for and read a response from the user blocking stdin from the "find .." command until I "read" a response from the user?

I thought putting a section of code withing parenthesis should fork a child process outside the influence of stdin from the parent process, but it does not appear that is the case.

Any ideas would be appreciated.
Do it right the first time and you will be ahead in the long run.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Posix shell scripting... prompting for a response from within a "while read" loop

Hi Kurt:

You need to establish another file descriptor to accomodate the read within the loop.

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

Regards!

...JRF...
steven Burgess_2
Honored Contributor

Re: Posix shell scripting... prompting for a response from within a "while read" loop

Hi

To create variable using read command you need

\c at the end of your echo within quotes ""

Is that what you mean ? -

I had a little trouble understanding exactly what you are after

Steve



take your time and think things through
Darrell Allen
Honored Contributor

Re: Posix shell scripting... prompting for a response from within a "while read" loop

Hi,

Try this:

find / ! -fstype nfs -only -perm -o+w -type d | while read line; do
echo "Do you want to change permissions on file ${line## *}."
read answer <&1
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
John Poff
Honored Contributor

Re: Posix shell scripting... prompting for a response from within a "while read" loop

Hello Kurt,

I have a favorite script that might solve your problem. The script has a function that takes yes/no input. You might do something like this:

# Get a yes/no answer to a question. Exit status 0 if "yes"
# Allow a -y or -n switch to give the default answer on just a newline
function getyesno
{
typeset ans
typeset default
if [[ ${1-} = -y ]] ; then default=0 ; shift ;
elif [[ ${1-} = -n ]] ; then default=1 ; shift ;
fi
while true
do
print -n >&2 "$@ "
read <&2 ans
case $ans in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
?*) ;;
*) # Answer was just
if [[ ! -z $default ]] ; then return $default ; fi
;;
esac
print >&2 " Please answer yes or no"
done
}


find / ! -fstype nfs -only -perm -o+w -type d | while read file
do
if $(getyesno -y "Do you want to change permissions on file $file [y]")
then
chmod o-w $file
fi
done





Or something like that.

JP


Kurt Renner
Frequent Advisor

Re: Posix shell scripting... prompting for a response from within a "while read" loop

James and Darrell had the answers I was looking for. Both solutions worked. Thanks! I knew there was a way to do it, but couldn't figure it out. In the past, I would always load an array first, if it was a short list, and then cycle through the array in a separate loop to prompt for answers.

In this case, the array subscripts could easily go beyond 1024 elements which made array usage a poor choice.

Thanks again!
Do it right the first time and you will be ahead in the long run.