Howto: Pause a Shell Script


Computer Data Output

Computer Data Output

File this one under “Another Nifty script-let”.  The problem this time is getting a shell script to pause for user input.  What I needed was a way to make a script stop, saying “Hit a key to continue..”, wait for the user to hit a key, then continue the script execution.  Here’s how:

Ok, I’m blaming this one on the shell.  Typically on the majority of Linux systems I use, the default shell is bash.  Now this is rather handy, when it comes to pausing shell scripts, as one can rely on the trusty “read -n 1″ command.  This will wait for a keypress before continuing.

While this is fine for any platform supporting bash, some platforms I need to support don’t have bash available, and instead use csh, ksh, and others.  What I needed was a way to pause a script which would work on any platform.  The result I arrived at is here:

#!/bin/sh
# Example script which asks the user to hit a key, then continues.

####################
# Functions
####################

# Waits for user key press.
Pause()
{
 OLDCONFIG=`stty -g`
 stty -icanon -echo min 1 time 0
 dd count=1 2>/dev/null
 stty $OLDCONFIG
}

####################
# Main Script
####################

echo "Hit a key to continue ..."
Pause
echo "OK, we'll carry on ..."

So line 1 defines our dependency on the default shell (/bin/sh).  Depending on the system this is usually symlinked to bash, csh, ksh, or whatever your sysadmin has decided your default shell to be.

The interesting part of the script is the function Pause on lines 9-15.  Here we backup the configuration of stty (line 11).  Stty is used to change the terminal settings, so on line 12 we request non-canonical input, no echo, etc.  See the man page for stty for more information.  Then we read one byte from stdin using dd (line 13).  Finally we restore the terminal settings on line 14.

Lines 21-23 show a simple script which uses this function.  Here we ask the user to press a key, and the Pause function takes care of waiting until a key is read, before we continue on our way.

The beauty of this script, is that it will run on bash, csh, ksh, etc., so my cross-platform concerns are mitigated.

About these ads

Tags: , , , , , , ,

5 responses to “Howto: Pause a Shell Script”

  1. jeison says :

    nice one, thanks for sharing!

  2. Chris says :

    Works great! Thanks!

  3. Peter Kerr says :

    awk “BEGIN{getline}” –
    does the job too

  4. Parvinder Singh says :

    But, using this way, the script will pause only where one has specified “pause” in script.

    If you want to pause a script anytime during its execution, just follow:

    http://parvindersingh.webs.com/apps/forums/topics/show/8637708-solved-unix-how-to-pause-and-resume-a-script-?next=

  5. Karolyn says :

    Greetings! Very helpful advice in this particular
    article! It’s the little changes that produce the largest changes. Thanks a lot for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 150 other followers

%d bloggers like this: