在使用管道的shell脚本中使用蚊子

问题描述 投票:0回答:1

我正在openWRT设备上使用mosquitto从服务器接收一些数据,然后将相同的数据发送到本地打印机以打印此数据。

我正在使用此脚本来接收数据

mosquitto_sub -h "${HOST}" -k 30 -c -p 8883 -t "${TOPIC}" -u "${USERNAME}" -P "${PASSWORD}" --id "${ID}" | /bin/sh /bin/printer_execute "${TOPIC}" "${PRINTER}" "${USERNAME}" "${PASSWORD}"

和printer_execute代码:

    #!/bin/sh

TOPIC="${1}"
PRINTER="${2}"
USERNAME="${3}"
PASSWORD="${4}"

while read MSG
  do
    echo "input: ${MSG}"
      echo "INPUT MSG: " "${MSG}" >> /root/log
      RES=`curl -m 2 --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction: ''" --header "If-Modified-Since: Thu, 01 Jan 1970 00:00:00 GMT" --data "${MSG}" "http://${PRINTER}/cgi-bin/epos/service.cgi?devid=local_printer&timeout=5000"`
      mosquitto_pub -h ${HOST_PLACEHOLDER} -p 8883 -t "${TOPIC}-response" -m "${RES}" -u "${USERNAME}" -P "${PASSWORD}"
    echo "RESULT CURL: " "${RES}" >> /root/log
done

此解决方案以每秒较低的消息速度工作,但是当音量太高时,printer_execute代码将停止工作。我对Shell脚本还很陌生,我猜问题可能是由管道和while读取模式或while退出条件引起的,但我不确定。

任何人都有想法或发现了类似的问题,并且知道如何解决此问题?

我正在openWRT设备上使用mosquitto从服务器接收一些数据,然后将相同的数据发送到本地打印机以打印此数据。我正在使用此脚本来接收数据...

shell mosquitto openwrt
1个回答
0
投票

您可以尝试制作一个函数来处理一条消息,然后在后台调用它

© www.soinside.com 2019 - 2024. All rights reserved.