Click to See Complete Forum and Search --> : mass redirection problem


morphman
06-12-2002, 11:38 AM
Hey its ME, your favorite question poster again. Btw, because i post yet another question does not necessarily mean i have closure on my other questions, hehe. So anyway....

My problem is with redirecting if, case, and while statements. If i do not redirect the while loop the following piece of code works perfectly:


while true
do
echo -n "Do you wish to install DISK5 for SCO? y/n: "
read disk5
echo

case $disk5 in
[yYnN]) ;;
*) echo "You did not answer with a \"y\" or an \"n\". Please try again."
echo
continue;;
esac
break
done


BUT if i add a " > `tty`" to the "done" at the end like this:


while true
do
echo -n "Do you wish to install DISK5 for SCO? y/n: "
read disk5
echo

case $disk5 in
[yYnN]) ;;
*) echo "You did not answer with a \"y\" or an \"n\". Please try again."
echo
continue;;
esac
break
done > `tty`


the code ceases to work. What happens is the first output line gets read "Do you wish to install DISK5 for SCO? y/n: " and no matter what the user enters the script exits back to the command prompt with a exit status of 0. Anyone have any idea what the heck is happening? I had a similar situation before where i had a case statement within a while loop and when i tried redirecting the while loop it did the same thing... but when i just redirected the case statement it worked. Help!

--Morphman, eternally grateful to those who bear the burden of my work, hehe

morphman
06-12-2002, 02:27 PM
Just something to add to that: when i redirect the case statement i doesnt actually work correctly either. Ex:


while true
do
echo "enter y or n: "
read answer
case $answer in
[yYnN]);;
*)echo you didnt enter a y or an n. please try again.
continue;;
esac > `tty`
break
done


If the user enters a "y" or an "n" then the script works fine. But if the user enters anything else besideds y or n then the while loop just exits. Why on earth would adding " > `tty`" cause this to happen? :confused:

Strogian
06-12-2002, 04:59 PM
Originally posted by morphman:
<STRONG>Just something to add to that: when i redirect the case statement i doesnt actually work correctly either. Ex:


while true
do
echo "enter y or n: "
read answer
case $answer in
[yYnN]);;
*)echo you didnt enter a y or an n. please try again.
continue;;
esac &gt; `tty`
break
done


If the user enters a "y" or an "n" then the script works fine. But if the user enters anything else besideds y or n then the while loop just exits. Why on earth would adding " &gt; `tty`" cause this to happen? :confused:</STRONG>

Hmm. That script works fine for me. Did you copy & paste that over, or did you actually retype it? If you retyped it, you may actually want to check your original code for some small error.

Strogian
06-12-2002, 05:04 PM
Your first script works perfectly here, too. You're using bash, right?

rbelt
06-12-2002, 07:48 PM
script works on my machine (i.e. second one w/tty)

\\RB

morphman
06-13-2002, 09:12 AM
Im using sh. No, i didnt copy that over, i retyped it. Hmm... Ill try retyping it but it happened in every instance that i used that (3 different spots) so that leads me to believe it may be something with the shell. Does anyone have access to sh? Im running SCO 5.0.6 openunix on this system. Thanks!

--Morphman, still working on it

PS: the worst case scenario is that i just do a:
sed '/echo/s/$/ &gt; `tty`/' myscript
and add the redirection after every single echo command which works fine. Its just ugly and i want to learn how to do it the better way!

Strogian
06-13-2002, 10:07 AM
Ah ha! I just tried it using sh, and I now have the exact same problem you are having. So it has something to do with a difference between bash and sh.

morphman
06-13-2002, 10:25 AM
and what is worse yet is that the script does not behave the same if i run it before login. Doh! I thought the only thing i would have to do to get it to work was to redirect every echo statement to `tty` and then the rest of the script would work normally. Well, sure enough i was wrong.

So before login my script runs and the first thing it does is ask:
Do you wish to install blah blah? y/n:

Then the user enters a "y". but it does not exit the loop. Instead it asks again:
Do you wish to install blah blah? y/n:
over and over and over... This is the same script that i showed an example of. Does anyone know the answers to any of these questions? Why cant everything just be cake? What is the meaning of life/linux?

--Morphman, frustrated as ever but still learning

Strogian
06-13-2002, 10:27 AM
All right, I was just fooling around with your second script there. The first thing that hit me was, "why is the break statement down there like that?" Here's what I changed:

while true
do echo "enter y or n: "
read answer
case $answer in
[yYnN])break;;
*)echo you didnt enter a y or an n. please try again.;;
esac
done


That works exactly the same as the original. But when I add the &gt;`tty` to esac, it ends up never breaking from the loop, no matter what I put. So, the &gt;`tty` apparently hinders the break and continue commands, somehow. I also tried changing 'break' to 'break 9' (to break 9 levels), and it still didn't work. So, this is pretty strange. ;)

morphman
06-13-2002, 10:34 AM
I know... its pretty strange and pretty sucky. :( There has to be some logical reason for this. But like i said i can get around it just by individually redirecting every echo command. But now there is a problem with this infinite looping....

Strogian
06-13-2002, 10:53 AM
Well, I did a little USENET search, and found this:
http://groups.google.com/groups?hl=en&lr=&selm=1316%40laidbak.UUCP

It may just be a known bug (although that post was made in 1988.. ;) ). That post illustrates that even the exit command will be ignored in such a situation. (just tested it too) So, I guess you'll have to use a different shell, or redirect all the commands separately.

And about it 'not running the same before login', what do you mean by that? Do you mean that it works fine (with every echo redirected) when you run it after login, but not if you run it before?

morphman
06-13-2002, 11:20 AM
Hey, thanks for the help!!

As for the other problem... If i put a script called P85myscript into the /etc/rc2.d directory it gets executed right before login. Lets say this script is the code i posted before:

while true
do
echo "enter y or n: "
read answer
case $answer in
[yYnN])break;;
*)echo you didnt enter a y or an n. please try again.;;
esac
done

What happens at login is the first line gets printed to the terminal:
"enter y or n: "
if you hit "y" then enter it just says:
"enter y or n: "
again... and again. it doenst matter what you hit. I dont know... i wouldnt bother trying to figure this one out for me. I think im just gonna say "buck it" and run the script after login (which dumbs down the script some but i dont particularly care at this point). If i find something out in the future ill come back and fix it. I hopefully wont have to use sh very often anyway. Thanks for the help!!

--Morphman