
Leslie Katz wrote:
<snip>
#!/bin/bash # Shows how to read a line from stdin
echo "Would you like to exit this script now?" read answer if [ "$answer" = y ] then echo "Exiting..." exit 0 fi
I tried that script. There's a tiny problem with it. It exits no matter what answer the user gives! The only difference between giving a "y" answer and any other answer is that the word "Exiting" doesn't appear if one doesn't give a "y" answer.
How should I amend my script to keep the output on the screen until the user enters some appropriate input?
You need to have something happening at the end of the script - if you look over your script, the if "answer" is "y", then you call exit ... otherwise the script ends (bascially calling exit for you) I've crufted this together ... I'm sure it's not completely portable, but should suit your purposes: ---[ Start Example ]--- #!/bin/bash # Shows how to read a line from stdin KEEPGOING=true echo "Would you like to exit this script now?" while $KEEPGOING; do read answer if [ "$answer" = y ] then echo "Exiting..." KEEPGOING=false fi; done; ---[ End Example ]--- YMMV, Hope all that helps, Cheers, Wazza.