读取时命令没有输出?

时间:2013-02-20 22:31:42

标签: android bash shell sh busybox

我有一个相当奇怪的问题。我无法弄清楚为什么会发生这种情况或如何解决它。

脚本:

#!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 _

单独运行会显示输出到stdout(应该在while循环中读取和修改的输出):

timeout -t 5 getevent -t

有什么想法吗?

感谢。

修改

好吧,所以我认为这是一个缓冲问题。基本上这给出了类似的问题: getevent > output 文件会在每一堆事件中更新,但不会立即更新(如果没有足够的事件,则可能永远不会更新)。我不知道在Android上有任何解决方法。

3 个答案:

答案 0 :(得分:2)

我认为无法在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

答案 1 :(得分:2)

这基本上就是我在你的代码中翻译过的东西,然后被bug解决方法所破坏。在超时分支上替换我没有cat的一个命令,它在Busybox sh,dash,mksh,bash和ksh93中正确运行您的示例输入。

由于关于shell和应用程序的许多基本知识都被打破了,所以这不起作用也就不足为奇了。我会确保Busybox是最新的,看看你保持命中的算术和其他错误是否可以在其他系统上重现,并报告错误,如果这段代码不起作用。

#!/bin/sh

reader() {
    t2=-1
    grep -vE '^(add device|[[:space:]]+name:)' | 
    while IFS=' ' read -r t1 a b c d _; do
        let "(t = (t1 = ${t1%%-*}) - t2) > 0 && t2 > -1" && echo "sleep $t"
        printf 'sendevent %s' "${a%[[:digit:]]:}"
        printf '%5d %5d %5d\n' "0x$b" "0x$c" "0x$d"
        let t2=t1
    done
}

let() {
    IFS=, command eval test '$(($*))' -ne 0
}

countDown() {
    echo 'Starting in...'
    i=4
    while let 'i-=1 > 0'; do
        echo "$i"
        sleep 1
    done
    printf '%s\n\n\n' 'Go!'
    echo "$*"
    "$@" <&3 | reader
}

isDigit() {
    while ! ${1+false}; do
        case $1 in
            *[^[:digit:]]*|'') return 1
        esac
        command shift
    done 2>/dev/null
}

main() {
    printf '%s >' 'Catch by [n]umber of events or [t]imeout?'
    read type

    case $type in
        n)
            printf '%s >' 'Events to catch?' 
            read arg
            isDigit "$arg" || return 1
            echo "Gonna catch $arg events!"
            countDown getevent -t -c "$arg"
            ;;
        t)
            printf '%s >' 'Timeout (in seconds)?' 
            read arg
            isDigit "$arg" || return 1
            echo "Gonna catch events for $arg seconds!"
            countDown busybox timeout -t "$arg" cat -
            ;;
        *)
            return 1
    esac
}

main "$@" 4<&0 <<\EOF 3<&0 <&4 4<&-
add device 1: /dev/input/event8
  name:     "bcm_headset"
add device 2: /dev/input/event7
  name:     "max8986_ponkey"
add device 3: /dev/input/event6
  name:     "sec_touchscreen"
add device 4: /dev/input/event5
  name:     "sec_keypad"
add device 5: /dev/input/event4
  name:     "orientation"
add device 6: /dev/input/event3
  name:     "accelerometer"
add device 7: /dev/input/event0
  name:     "proximity_sensor"
add device 8: /dev/input/event2
  name:     "geomagnetic_raw"
add device 9: /dev/input/event1
  name:     "geomagnetic"
45534-48646 /dev/input/event6: 0001 008b 00000001
45534-48646 /dev/input/event6: 0000 0000 00000000
45534-48646 /dev/input/event6: 0001 008b 00000000
45534-48646 /dev/input/event6: 0000 0000 00000000
EOF

# vim: set fenc=utf-8 ff=unix ft=sh :

输出:

 $ bb --help
BusyBox v1.21.0 (2013-02-20 20:39:21 CST) multi-call binary.

Usage: bb [-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]

Unix shell interpreter
 $ bb answers/countdown
Catch by [n]umber of events or [t]imeout? >t
Timeout (in seconds)? >5
Gonna catch events for 5 seconds!
Starting in...
3
2
1
Go!


busybox timeout -t 5 cat -
sendevent /dev/input/event6:    1   139     1
sendevent /dev/input/event6:    0     0     0
sendevent /dev/input/event6:    1   139     0
sendevent /dev/input/event6:    0     0     0

答案 2 :(得分:0)

有同样的问题,并记得我第一次在MS-DOS 3.30中使用的技巧,以及后来在Cygwin中使用的技巧(不是为了超时,但是更多的命令是许多重定向问题的神奇子弹):

timeout -t 8 getevent | more > getevent.txt

在Total Commander的shell中运行良好 用su(不是sh)

欢迎您: - )