我尝试基于MQTT主题执行etherwake命令。 如果将mosquitto_sub的输出通过管道传递到while语句中,则会停止输出。
可运行的示例:
# mosquitto_sub -L mqtt://... | grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}'
00:00:00:00:de:ad
00:00:00:00:be:ef
00:00:00:00:ca:fe
(goes on and on)
无法工作:
mosquitto_sub -L mqtt://... \
| grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}' \
| hexdump
输出在一行之后停止:
0000000 1234 5678 9abc def0 abcd cafe 3762 3a65
大局观是这样的:
mosquitto_sub -L mqtt://... \
| grep -o -E '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}' \
| while read macaddr; do
echo "send WOL to " $macaddr;
/usr/bin/etherwake -D -b "$macaddr" 2>&1;
done
通常情况下我对Linux shell很熟悉,但这一次在第一行之后它变得卡住了。
我的猜测是stdin或stdout出现了问题(未被读取或已满等),但我想不出原因。
顺便说一句,这是一个OpenWRT shell,所以是ash而不是bash。
grep
的标准输出积累约4kb左右的内容,或者在它没有将期望的内容写入输出时就无限期地调用它? - Charles Duffyetherwake
也在读取仅由read
读取的管道。它可能会忽略该流并且不消耗任何数据,但请确保您真的需要执行while read macaddr; do ...; /usr/bin/etherwake -D -b "$macaddr" 2>&1 <&-; ...
并关闭etherwake的stdin(或将其重定向从/dev/zero或/dev/null等)。 - William Pursell