LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   wait a keypress using the command read (http://www.linuxquestions.org/questions/programming-9/wait-a-keypress-using-the-command-read-4175456426/)

yarock 04-01-2013 04:21 PM

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?

cbtshare 04-01-2013 05:32 PM

please post the contents of data.txt

Habitual 04-01-2013 05:44 PM

Code:

...
echo "Press any key to continue"
read ANSWER
...


jpollard 04-01-2013 05:48 PM

Quote:

Originally Posted by yarock (Post 4923046)
...
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".

Kenhelm 04-01-2013 07:37 PM

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


towheedm 04-01-2013 09:39 PM

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.


All times are GMT -5. The time now is 11:57 AM.