在Bash中阅读一段时间内的完整消息



i通过/dev/tcp连接到服务器,以接收传入的XML消息。下面的脚本运行良好,但是如果XML消息中有空白字符,则该消息是突然的。但是我需要阅读完整的消息。我可以通过在while-loop中集成关闭XML标签来解决此问题?

#!/bin/bash
exec 3<>/dev/tcp/192.168.24.23/1234
processResponse() {
  RESPONSE=$1 
  if [[${RESPONSE:0:10} == "<firsttag>" ]]; then
    curl -X POST --data "postdata=$RESPONSE" http://localhost/index.php
  fi 
}
while read response
    do processResponse $response
done <&3

XML-Message将就像:

<firsttag>
   <secondtag>message with blanks inside</secondtag>
</firsttag>

正如Chepner所说的那样,您的方法并不合适,但是如果您必须放弃XML解析器,则可以将其解决此案例。我不是脚本脚本while循环,而是使用awk来读取和组装消息行。

awk '/<firsttag>/   { RESPONSE = "" }
     /<firsttag>/,/</firsttag>/    { RESPONSE = RESPONSE$0 }
     /</firsttag>/ { system("curl -X POST --data "postdata="RESPONSE"" http://localhost/index.php") }
    ' <&3

最新更新