LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-01-2013, 04:21 PM   #1
yarock
LQ Newbie
 
Registered: Apr 2013
Posts: 2

Rep: Reputation: Disabled
Question wait a keypress using the command read


Hi, at a certain point in my bash script, I want to wait for a key pressed on the keybord. I searched on the net and I found that I should use the command read -s -n 1 . Here is the format of my script:

#########################################
1- while read var1 var2
2- do
3- echo "hello"
4-
5- if [ var3 -eq var4 ]; then #var3 and var4 are defined before
6- echo "Press any key to continu"
7- read -s -n 1
8- fi
9-
10- done < data.txt
##########################################

My problem is that when the script execute the line 7, instead of waiting for a keypress, it use the next caracter of data.txt and it continu until the end of the file data.txt. How can I force it to use a keypress?

Last edited by yarock; 04-01-2013 at 04:23 PM.
 
Old 04-01-2013, 05:32 PM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 561

Rep: Reputation: 42
please post the contents of data.txt
 
Old 04-01-2013, 05:44 PM   #3
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,156
Blog Entries: 4

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Code:
...
echo "Press any key to continue"
read ANSWER
...
 
Old 04-01-2013, 05:48 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,115

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Quote:
Originally Posted by yarock View Post
...
7- read -s -n 1
8- fi
9-
10- done < data.txt
##########################################

My problem is that when the script execute the line 7, instead of waiting for a keypress, it use the next caracter of data.txt and it continu until the end of the file data.txt. How can I force it to use a keypress?
That is because "read" is getting its input from "stdin", which is redirected from "data.txt".
 
Old 04-01-2013, 07:37 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Try one of these:
Method 1: using a file descriptor
Code:
1- while read -u9 var1 var2
2- do
3- echo "hello"
4- 
5- if [ var3 -eq var4 ]; then #var3 and var4 are defined before
6- echo "Press any key to continu"
7- read -s -n 1 
8- fi
9- 
10- done 9< data.txt
Method 2: using /dev/tty
Code:
1- while read var1 var2
2- do
3- echo "hello"
4- 
5- if [ var3 -eq var4 ]; then #var3 and var4 are defined before
6- echo "Press any key to continu"
7- read -s -n 1 < /dev/tty
8- fi
9- 
10- done < data.txt
 
Old 04-01-2013, 09:39 PM   #6
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 585

Rep: Reputation: 118Reputation: 118
Please use CODE tags for your code. It preserves formatting and makes it much easier to read.

You have two read statements in your while loop. The first read will read line n with the second read getting the contents from line n+1.

You can specify which file descriptor read gets it input from with the u option. Since you want to read from stdin, whose file descriptor is 1 you'll use:
Code:
read -u 1
You can also use the p option to show a prompt. So you code will be:
Code:
while read var1 var2 ; do
  echo "Hello"

  if [ "$var3" -eq "$var4" ] ; then
    read -p "Press any key to continue" -u 1
  fi
done < data.txt
Note also that var3 and var4 are variables and as such must be specified accordingly.

Hope it helps.

Last edited by towheedm; 04-01-2013 at 09:41 PM.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looking to induce high i/o read/wait for testing gargunkle Linux - Software 1 11-29-2011 08:28 PM
Read keyboard status (not: wait for keyboard input). stf92 Programming 14 07-03-2011 05:55 PM
[SOLVED] Read from stdin in perl, do not wait for EOF smeezekitty Programming 1 04-28-2010 03:17 PM
Does Kornshell wait until one command completes before running the next command? ShaqDiesel Programming 4 09-10-2008 11:49 PM
wait for x seconds type command sub-genius Slackware 1 07-14-2003 07:11 PM


All times are GMT -5. The time now is 05:18 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