How to read input after calling program, gives: "read: read error: 0: Resource temporarily unavailable"
I'm calling a program called arecord (see the code below). It takes in micr开发者_如何学JAVAophone input, and I stop it by pressing Ctrl+C to continue with the rest of the script.
I next want it to do is read my input (c or r) to see whether it should break out of the loop
However, the input isn't read, and the error "read: read error: 0: Resource temporarily unavailable".
I guess it has something to do with the exit code or input stream, but I can't go further with it.
#!/bin/bash
while :
do
# Record the audio
arecord -f cd -c 1 -t wav sound.wav
# Recording now finished, get user input
read -p "Continue or repeat recording? [c, r]: " input
if [ "${input}" == "c" ]
then
break
fi
done
The answer is not simple, it depends how arecord
handles SIGINT
(ctrl+c), read this if you want to understand what exactly is happening. Anyway, i would recommend not to use SIGINT in that way, but:
run arecord
in background (with &
at the end), get the pid (arecodr_pid=$!
), then read
something like enough
, kill -2 $arecodr_pid
and after that you can do # Recording now finished, get user input
If arecord
knows to handle other signals, you can use kill -<signal num> $arecord_pid
Do not use -9, processes can not handle it and in most cases your out file will be corrupted.
You could try to read directly from tty:
read -p "Your prompt " -r </dev/tty
instead of simply:
read -p "Your prompt " -n 1 -r
In a script having a similar issue, it worked.
精彩评论