LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to make shell script wait for key press to proceed... (https://www.linuxquestions.org/questions/linux-general-1/how-to-make-shell-script-wait-for-key-press-to-proceed-687491/)

ddenton 12-01-2008 11:26 AM

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...

colucix 12-01-2008 11:38 AM

You can use the read statement. Do you expect the user press a particular key or any key is good to proceed?

tgerbert 12-01-2008 11:38 AM

Quote:

Originally Posted by ddenton (Post 3360658)
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).

ddenton 12-01-2008 11:38 AM

I think I've found the solution, a combination of "read" and "echo". Thanks anyway!

ddenton 12-01-2008 11:40 AM

Thanks all for the replies. That was much faster than I'm used to... :)

Colucix, any key pressed would suffice... What's your suggestion?

colucix 12-01-2008 11:40 AM

Quote:

Originally Posted by ddenton (Post 3360683)
I think I've found the solution, a combination of "read" and "echo". Thanks anyway!

Also check read -p. You will save the echo.

colucix 12-01-2008 11:42 AM

Quote:

Originally Posted by ddenton (Post 3360686)
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

repo 12-01-2008 11:43 AM

read -p "Press any key..."

ddenton 12-01-2008 11:48 AM

I just found the -p switch too, and it does exactly what I need it to do. Thanks all for your input!

fiomba 12-01-2008 12:01 PM

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.

tredegar 12-01-2008 12:46 PM

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.

ddenton 12-01-2008 01:31 PM

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.

Disillusionist 12-01-2008 02:30 PM

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


tredegar 12-02-2008 04:25 AM

Disillusionist,
That's much better. Thank you :)


All times are GMT -5. The time now is 01:05 AM.