我正在尝试修改在https://retropie.org.uk/forum/topic/17924/detect-idle-state-power-off-screen处找到的代码,以便它可以同时监视两个js输入。我该如何实现?
我正在为此部分苦苦挣扎
inputActive=$(
timeout ${inputWindow} \
dd \
if=/dev/inputs/js0 \
of=/dev/null \
count=${inputCount} \
>/dev/null 2>&1;
echo $?
)
if [ ${inputActive} -eq 0 ]; then
因此,如果js0上有活动,它将返回0。 我想要类似的东西
inputActive=$(
(
timeout ${inputWindow} \
dd \
if=/dev/inputs/js0 \
of=/dev/null \
count=${inputCount} \
>/dev/null 2>&1;
echo $?
);
(
timeout ${inputWindow} \
dd \
if=/dev/inputs/js1 \
of=/dev/null \
count=${inputCount} \
>/dev/null 2>&1;
echo $?
)
)
一旦发现任何输入上的活动,它就应该走得更远,而不必等到所有任务都完成了。
答案 0 :(得分:1)
在后台运行这两个命令,并使用wait -n
等待直到其中一个完成。 (摆脱整个inputActive=$(...; echo $?)
业务。它没有做任何有用的事情。)
timeout "$inputWindow" dd if=/dev/inputs/js0 of=/dev/null count="$inputCount" &> /dev/null &
timeout "$inputWindow" dd if=/dev/inputs/js1 of=/dev/null count="$inputCount" &> /dev/null &
wait -n
如果要检查是否成功,可以直接在wait
语句中使用if
:
if wait -n; then
echo "one of them succeeded"
else
echo "one of them failed" >&2
fi
顺便说一句,您可以使用read
代替timeout
和dd
。 read -N
将读取一定数量的字符,并且read -t
设置超时。
read -N "$inputCount" -t "$inputWindow" < /dev/inputs/js0 &
read -N "$inputCount" -t "$inputWindow" < /dev/inputs/js1 &
wait -n