making a .sh wait for user input

I need a script to halt at the end and wait for the user to hit a key...could be any ket or enter. I know it can be done but I am just starting out.. Thanks

echo "Hit return to continue"
read dummy_variable

To allow them to continue after hitting any key, I usually create a function (I usually call it readOne, after the function I stole the idea from :stuck_out_tongue: ):

readOne () {
tput smso
echo "Press any key to return \c"
tput rmso
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 >/dev/null 2>&1
stty "$oldstty"
echo
}

The call it later from the script:

blah blah ...
readOne
blah blah
exit 0

HTH

Interesting use of dd. Nice post.

Very interesting. I've never seen a way to read a single character from a shell script before. But I want to be able to actually catch the character that is typed. So I changed the dd statement to:
result=`dd bs=1 count=1 2>/dev/null`
which seems to be working. Very cool trick!

Hi,

Well I was trying to see what exactly the script Livinfree posted is doing.

I couldnt understand.

Using man I could get that tput smso and rmso is for getting standout mode seq started and end it.
dd bs sets the blcok size to 1.But why I didnt undertand. Is it because the requirement is that only ONE keystroke is required.

however I couldnt understand how the rest of these work when put together.

Could anyone pls explain.

Thanks.

and I am a fool it seems...Was looking at todays thread and dont know why I ended up on a thread 2 years old!
Sorry folks....Ignore my previous mail... by mistake I thought this thread is new.

Thanks

Don't apologize. There is nothing wrong with posting a followup to an old thread. The only problem is it would take a book to really answer your question. I'll take a shot at a summary.

The tty driver controls how input lines that you type are delivered to programs. Normally a tty driver will wait until a complete line is available. It also handles stuff like backspace so that program doesn'y need to. The stty command lets you change the way the tty driver works. "stty -a" will display all of the settings. You should do that to see what's available.

"stty -g" displays all of the settings too. But it's encoded and you can't understand the output. But you can save the output and feed it back into the stty command. So:
oldtty=`stty -g`
stty $oldstty
will save and restore the original settings of the tty driver.

stty -canon -echo min 1 time 0
is setting some options in the tty driver. -icanon turns off all special character processing. Now a backspace will be passed to the program rather than being processed. And a carriage return won't terminate a line. So now min and time control when a read has finished. "min 1" says we need at least one character. "time 0" means that we won't wait a while before completing a read. So each read from the program may return after just one character.

the -echo just turns off echo. I would not have done that.

dd is a program that is prepared to read data that is not organized into lines. This dd will read one block (count=1) of data. And that block will be one character in length (size=1). So the dd will read one character and return to the script.

The final echo moves the cursor to the next line.

and i was just gonna say, i havnt seen livinfree post in a while...

:slight_smile:

Well thanks Perderabo!
That clears a lot of doubts I had regarding the script.

Thanks