認爲這會更明顯,但我需要在按下鍵時運行命令,並在不再按下命令時停止命令(SIGINT)。在Bash鍵不再被按下時停止命令
#!/bin/bash
while [ F5 is being pressed ] ; do
arecord -f cd -t wav
done
認爲這會更明顯,但我需要在按下鍵時運行命令,並在不再按下命令時停止命令(SIGINT)。在Bash鍵不再被按下時停止命令
#!/bin/bash
while [ F5 is being pressed ] ; do
arecord -f cd -t wav
done
轉發,你可以有:
#!/bin/bash
shopt -s extglob
KEYS_F5=$'\e[[E'
KEYS_XTERM_F5=$'\e[15~'
function keys_readonce {
__V0='' __V1=''
local A K
local -a KEY=() S=() T=()
for A; do
case "$A" in
+([[:digit:]]))
T=(-t "$A")
;;
-q|--quiet)
S=(-s)
;;
*)
echo "Invalid argument: $A"
exit 1
;;
esac
done
local IFS=''
if read -rn 1 -d '' "${T[@]}" "${S[@]}" K; then
KEY[0]=$K
if [[ $K == $'\e' ]]; then
if [[ BASH_VERSINFO -ge 4 ]]; then
T=(-t 0.05)
else
T=(-t 1)
fi
if read -rn 1 -d '' "${T[@]}" "${S[@]}" K; then
case "$K" in
\[)
KEY[1]=$K
local -i I=2
while
read -rn 1 -d '' "${T[@]}" "${S[@]}" "KEY[$I]" && \
[[ ${KEY[I]} != [[:upper:]~] ]]
do
((++I))
done
;;
O)
KEY[1]=$K
read -rn 1 -d '' "${T[@]}" 'KEY[2]'
;;
[[:print:]]|$'\t'|$'\e')
KEY[1]=$K
;;
*)
__V1=$K
;;
esac
fi
fi
__V0="${KEY[*]}"
return 0
fi
return 1
}
while keys_readonce 5 && [[ $__V0 == "$KEYS_F5" || $__V0 == "$KEYS_XTERM_F5" ]]; do ## 5 secs. timeout is optional
# Do something
:
done
在第14行中得到一個「意外的令牌」錯誤 - 沒有進入這種深度Bash之前,所以不知道有什麼:/ –
我忘了啓用'extglob'。現在已修好。 – konsolebox
你不想運行'arecord'到'F5'被釋放?在你的例子中,你會得到許多並行的'arecord'進程。 – jm666
是的,'arecord'繼續作爲一個單獨的過程,直到'F5'被釋放。我知道,我的例子的所有錯誤和一個while循環並不是真正的我所追求的,至少不是這樣的例子。 –