LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-01-2008, 11:26 AM   #1
ddenton
Member
 
Registered: May 2007
Posts: 114

Rep: Reputation: 15
How to make shell script wait for key press to proceed...


Hello.

I'm looking for a way to make my bash shell script stop at certain steps, and continue when a key is pressed. Maybe I'm not searching for the right terms, but google hasn't returned anything useful.

Any help is appreciated...
 
Old 12-01-2008, 11:38 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You can use the read statement. Do you expect the user press a particular key or any key is good to proceed?
 
Old 12-01-2008, 11:38 AM   #3
tgerbert
LQ Newbie
 
Registered: Jul 2008
Distribution: Ubuntu Studio 8.04
Posts: 15

Rep: Reputation: 0
Quote:
Originally Posted by ddenton View Post
Hello.

I'm looking for a way to make my bash shell script stop at certain steps, and continue when a key is pressed. Maybe I'm not searching for the right terms, but google hasn't returned anything useful.

Any help is appreciated...
Bash has a builtin command, "read" (check out 'man read'). You can cause bash to wait for a keypress by using 'read -n 1 -s'

The -n 1 tells read to only read one character (rather than waiting for an ENTER keypress before returning), and -s tells it to be silent (so the key pressed is not echoed on the terminal).
 
Old 12-01-2008, 11:38 AM   #4
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
I think I've found the solution, a combination of "read" and "echo". Thanks anyway!
 
Old 12-01-2008, 11:40 AM   #5
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks all for the replies. That was much faster than I'm used to...

Colucix, any key pressed would suffice... What's your suggestion?
 
Old 12-01-2008, 11:40 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by ddenton View Post
I think I've found the solution, a combination of "read" and "echo". Thanks anyway!
Also check read -p. You will save the echo.
 
Old 12-01-2008, 11:42 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by ddenton View Post
Colucix, any key pressed would suffice... What's your suggestion?
Exactly what tgerbert has suggested above, adding the -p option to prompt the user:
Code:
read -p "Press any key to continue... " -n1 -s
 
Old 12-01-2008, 11:43 AM   #8
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,496

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
read -p "Press any key..."
 
Old 12-01-2008, 11:48 AM   #9
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
I just found the -p switch too, and it does exactly what I need it to do. Thanks all for your input!
 
Old 12-01-2008, 12:01 PM   #10
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
Use read command:
Code:
#!/bin/bash
read var_year
echo "The year is: $var_year"

echo -n "Enter your name and press [ENTER]: "
read var_name
echo "Your name is: $var_name"

echo -n "Press [ENTER] to continue,...: "
read var_name
echo "You can go on!...."
Modify as you like and insert in your script.
 
Old 12-01-2008, 12:46 PM   #11
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
WARNING: slightly off-topic:

ddenton, you really need to read man bash - it has lots of useful information.
I prefer PDFs to the terminal manpages, especially as each is easily searchable within kpdf

Here's a little script to turn manpages into PDF files.
There's no error checking. And you had better not have important files called foo.ps or foo.pdf in your current directory, because they'll be removed.
Code:
tred@pc:~$ cat man2pdf
#!/bin/bash
# A very sloppy script to generate man pages as pdf files
man -t $1 > foo.ps && ps2pdf foo.ps && rm foo.ps && mv foo.pdf $1.pdf
kpdf $1.pdf &
tred@pc:~$
I just saved the script to my home directory, and made it executable.
You'll need to install ps2pdf which for my distro (Kubuntu) comes as part of the package ghostscript. Just use your package manager.

Use it like this: ./man2pdf bash

Enjoy.
 
Old 12-01-2008, 01:31 PM   #12
ddenton
Member
 
Registered: May 2007
Posts: 114

Original Poster
Rep: Reputation: 15
Tredegar,

I have referred to "man bash" in the past for other shell scripting questions, but the name of the command I needed escaped me.

That being said, I really do appreciate everyone's help, even the suggestion to turn man pages into PDF's. It's something I hadn't even though of. Thanks again!

P.S. - Once I knew the "read" command was what I needed, I referred to my "Linux in a Nutshell". I'd highly recommend it! It's pretty much man pages in a book.

Last edited by ddenton; 12-01-2008 at 01:35 PM.
 
Old 12-01-2008, 02:30 PM   #13
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Tredeger, I hope you don't mind but I've taken the liberty of modifying your code to add some basic error checking etc:

Code:
#!/bin/bash
###
###
usage(){
   echo "usage:- man2pdf [manpage]"
}

create_pdf(){
   man -t $manpage > /tmp/${manpage}.ps && ps2pdf /tmp/${manpage}.ps
   if [ $? -ne 0 ]
   then
      ## even if the ${manpage} man page doesn't exist
      ## /tmp/${manpage}.ps would have been created, so tidy-up
      rm -f /tmp/${manpage}.ps
      exit 1
   else
      ## remove temporary file
      rm -f /tmp/${manpage}.ps
   fi
}

manpage=$1

if [ "$manpage" == "" ]
then
   usage
   exit 2
fi

if [ -f ${manpage}.pdf ]
then
   read -p "${manpage}.pdf file exists, do you want to replace it?:" ans_yn
   case "$ans_yn" in
      [Yy]|[Yy][Ee][Ss]) echo "Replacing ${manpage}.pdf ...";;

      *) exit 3;;
   esac
fi

create_pdf

Last edited by Disillusionist; 12-01-2008 at 02:32 PM.
 
Old 12-02-2008, 04:25 AM   #14
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Disillusionist,
That's much better. Thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ g++ wait for key press??? blizunt7 Programming 18 05-19-2014 11:09 AM
Wait for one of two processes to complete in a shell script nonoitall Programming 11 06-10-2008 04:10 PM
How to make mid-mouse button send key press to application utahnix Linux - Software 2 10-09-2007 05:14 PM
How to make a script wait for input farmerjoe Linux - General 2 12-28-2004 05:18 AM
Make script wait for input farmerjoe Linux - General 4 12-28-2004 01:49 AM


All times are GMT -5. The time now is 08:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration