Getting user to choose whether to exit bash script

I've created a number of bash scripts which make it easier for me to run particular security applications, among them rkhunter. I run the scripts in gnome-terminal. It's crucial to say at the outset that I've had no tuition in bash script writing, so I'm sure my scripts won't win any beauty contest. In any event, I was happy with my script for running rkhunter, except for one thing. I had great difficulty reading part of the output of the script, because that output (from rkhunter itself) was in yellow on a white background. I then figured out how to write a script that opened a second window in gnome-terminal. That second window had a black background, in which my original script for running rkhunter then ran. Legibility of the yellow and all other output in the second window is fine. However, there's a problem. When my script for running rkhunter in the second window finishes, it automatically exits, leaving me back at the first window, so that I haven't got a chance to examine the rkhunter output except as it scrolls by rapidly in the second window. I therefore want to amend my script for running rkhunter so that it doesn't end until the user says so. In a number of places on the Web appears the following script, which I thought I could borrow: #!/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?

On Mon, 2006-01-02 at 17:53 +1100, Leslie Katz wrote:
#!/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
Bash often has problems with the keyboard buffer not being completely emptied, so you end up with the read being completed immediately with the results of the buffer. Add the following just before the read to empty the buffer. while :; do read -t 0; if [ "$?" -eq "1" ]; then break; fi; done It basically just loops reading for 0 seconds until read says there is nothing left in the buffer to read. There are probably more concise ways of doing it, but this is the clearest way I've found. Cheers -- Matt Brown matt(a)mattb.net.nz Mob +64 275 611 544 www.mattb.net.nz

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.
participants (3)
-
Leslie Katz
-
Matt Brown
-
Warren