Controlling user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Controlling user input
# 1  
Old 08-28-2019
Controlling user input

I'm trying to use a bash script for a psych experiment that involves listening to sound files and responding. If I have something like the code below, how can I make sure that a key press is assigned to RESPONSE only after the second echo statement?

Code:
for i in 1 2 3; do
 echo "Ready?"
 sleep 2
 mpg123 $i.mp3
 echo "Ok now you can respond. Press 1 if you liked it, 2 if you didn't."
 read -n 1 RESPONSE
done

Also, maybe this is the same question, but how can I make sure that if there are accidentally two or more key presses, that the extra presses are ignored instead of getting assigned to RESPONSE on the next iteration(s)?
# 2  
Old 08-28-2019
It is pretty easy in C with fseek(stdin, 0, SEEK_END);, but I don't know of any portable (or even any way without explicit keyboard actions initiated by the user at the terminal) way to do that with shell code.

Have you considered writing this in C?

Note also that asking a question (e.g. Ready?) and expecting users not to respond seems like you're asking for unwanted input.
# 3  
Old 08-28-2019
Since you specify using bash shell we can flush the input buffer using:

Code:
while read -rs -N 1 -t 0.1 2> /dev/null ; do : ; done

so your script would look like this:

Code:
#!/bin/bash

for i in 1 2 3; do
 echo "Ready?"
 sleep 2
 mpg123 $i.mp3
 echo "Ok now you can respond. Press 1 if you liked it, 2 if you didn't."
 while read -rs -N 1 -t 0.1 2> /dev/null ; do : ; done
 read -n 1 RESPONSE
done

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 08-29-2019
Quote:
Originally Posted by Chubler_XL
Since you specify using bash shell we can flush the input buffer using:

Code:
while read -rs -N 1 -t 0.1 2> /dev/null ; do : ; done

so your script would look like this:

Code:
#!/bin/bash

for i in 1 2 3; do
 echo "Ready?"
 sleep 2
 mpg123 $i.mp3
 echo "Ok now you can respond. Press 1 if you liked it, 2 if you didn't."
 while read -rs -N 1 -t 0.1 2> /dev/null ; do : ; done
 read -n 1 RESPONSE
done

Hi Chubler_XL,
That might work with some versions of bash, but it won't work with 3.2.57 (which comes with the most recent release of macOS Mojave) since the read built-in in that version doesn't have a -N option. The loop you suggest above silently throws away the diagnostic saying there is no -N option and gives no indication that whatever was intended to be done by the while loop didn't do anything. The following read then reads the next available character no matter when it was typed.

Since darwin_866 has told us neither what OS is being used nor which version of bash is being used, we don't have any way to know if what you suggested might work in his/her environment.

Cheers,
Don
# 5  
Old 08-29-2019
Works great in ubuntu linux with bash 5.0.3(1)-release. Thanks both!
This User Gave Thanks to darwin_886 For This Post:
# 6  
Old 08-29-2019
Quote:
Originally Posted by darwin_886
Works great in ubuntu linux with bash 5.0.3(1)-release. Thanks both!
I also tested the script and it worked fine for me on Ubuntu.

Code:
ubuntu:/tmp# read --help
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

Seemed to "kinda work" on macOS if I changed the -N to -n but then seemed to have an issue with the -t option so I moved on to other tasks.

Code:
mojave_10_14_6:tmp $ read --help
-bash: read: --: invalid option
read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]

Glad you got it working darwin_886.
# 7  
Old 08-29-2019
BASH allows you to use the basic TTY properties to establish timeouts and read minimums:

Code:
#!/bin/bash

# Timeout is 1/10 of a second, minimum read is 0 characters
stty -icanon min 0 time 1

sleep 3 & # Background command

# Eat any characters until 'sleep' quits
while [ -d /proc/"$!" ] ; do read G ; done

wait

printf "%s" "OK, now respond 1 or 2: "

while true
do
        case "$VAR" in
        1|2)    break           ;;
        *)      read VAR        ;;
        esac
done

echo
echo "You responded:  $VAR"

stty sane # Reset terminal to normal

Haven't got it working in KSH yet. Can't convince it to not mess with the TTY.
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Red Hat

"rhgb quiet" controlling the display of commands in single user mode ?"rhgb quiet" controlling the d

Why does removing "rhgb quiet" from the kernel boot parameters control whether or not the commands I enter are displayed in single user mode ? For instance, if I do not remove "rhgb quiet", when I am in single user mode, whatever command I type will not be displayed on the screen. The... (0 Replies)
Discussion started by: Hijanoqu
0 Replies

3. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

4. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

User input - Perl need Help

If I want all user input to start with " : " if not display error or what I asking is how to do if statement that control a first letter of string that we want to start with. and not worry about the rest Thank (1 Reply)
Discussion started by: guidely
1 Replies

7. UNIX for Dummies Questions & Answers

Validating user input

I'm trying to set up a script that takes user input and validates that the user input was entered correctly. So far I have this: while : do echo "Please enter your name." read NAME if then echo "You have not entered a name." echo... (13 Replies)
Discussion started by: fufaso
13 Replies

8. Shell Programming and Scripting

Question about user input

Hello all, How can i have a user input that reads like this: echo -n "Please enter a & b:" 10 20 read a read b echo $a echo $b 10 20 right now i have divided it into two echos, like echo -n "a: " echo -n "b: " (1 Reply)
Discussion started by: rrahmegni
1 Replies

9. UNIX for Dummies Questions & Answers

accept user input?

how would i accept user input from the keyboard? (2 Replies)
Discussion started by: JamieMurry
2 Replies

10. Shell Programming and Scripting

Getting user input

I am trying to create a shell (ksh) which has two "read" commands, one which reads a line from a file and another which is inside a loop that reads user input from a keyboard. However, the "read" command inside the loop uses the input from the file and it does not get the user input from keyboard.... (3 Replies)
Discussion started by: stevefox
3 Replies
Login or Register to Ask a Question