我有一个相当奇怪的问题。我不明白为什么会发生这种情况或如何解决它。
脚本:
#!system/bin/sh
#set -x
reader() {
t2=-1
grep -v -E "add device|name:" | while IFS=' ' read -r t1 a b c d _; do
t1=${t1%%-*}
t=`expr $t1 - $t2`
if let 't > 0 && t2 > -1'; then
echo "sleep $t"
fi
printf 'sendevent %s' "${a%?}"
printf '%5d %5d %5d\n' "0x$b" "0x$c" "0x$d"
t2=$t1
done
}
let() {
IFS=, command eval [ '$(($*))' -ne 0 ]
}
countDown() {
echo 'Starting in...'
i=4
while [[ $i -gt 1 ]]; do
i=$(($i-1))
echo "$i"
sleep 1
done
printf '%s\n\n\n' 'Go!'
# echo "$*"
"$@" | reader
}
clear
printf '%s >' 'Catch by [n]umber of events or [t]imeout?'
read type
case $type in
n)
printf '%s >' 'Events to catch?'
read arg
echo "Gonna catch $arg events!"
countDown getevent -t -c "$arg"
;;
t)
printf '%s >' 'Timeout (in seconds)?'
read arg
echo "Gonna catch events for $arg seconds!"
countDown timeout -t $arg getevent -t
esac
脚本的目标:
使用 getevent 命令捕获用户的交互(例如按键、屏幕点击等)并将脚本输出到 stdout,该脚本可用于复制这些事件。
其他信息:
getevent的输出是十六进制格式 sendevent 接受十进制格式
预期输出:
Catch by [n]umber or by [t]imeout? n
Events to catch? > 4
Gonna catch 4 events...
Starting in...
3
2
1
Go!
sendevent /dev/input/event5 1 102 1
sendevent /dev/input/event5 0 0 0
sleep 3
sendevent /dev/input/event5 1 102 0
sendevent /dev/input/event5 0 0 0
问题:
当选择“n”时,代码按预期运行。当选择“t”时,脚本将在指定的秒数后退出。但是,它不输出任何内容 - while 循环的第一行甚至不运行。
使用 set -x,显示的内容如下(最后几行):
+ printf %s\n\n\n Go!
Go!
+ reader
+ t2=-1
+ grep -v -E add device|name:
+ timeout -t 5 getevent -t
+ IFS = read -r t1 a b c d _
单独运行它会显示输出到标准输出(应该在 while 循环内读取和修改的输出):
timeout -t 5 getevent -t
有什么想法吗?
谢谢。
编辑:
好吧,所以我认为这是一个缓冲问题。基本上这给出了类似的问题:
getevent>输出
该文件会在每一组事件中更新,但不会立即更新(如果没有进行足够的事件,则可能永远不会更新)。我不知道 Android 上有任何解决办法。
最佳答案
我认为没有办法在 busybox 的 grep
中启用行缓冲,但你可以这样做:
$ adb shell timeout -t 10 getevent -t | \
grep --line-buffered -v -E "add device|name:" | \
while read line; do echo "READ: $line"; done
关于android - 读取命令时没有输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14991200/