将带有特殊字符的参数传递给 Socat exec 脚本

Passing parameter with special characters to Socat exec script

我有一个 RNDIS USB 调制解调器,它允许通过 telnet 端口 (5510) 发出 AT 命令。我想用它从脚本中读取和发送 SMS 消息。 我使用了发布的类似问题的变体 here 但我使用的是 socat 而不是 netcat。

  1. 我有一个名为 atread.sh 的帮助脚本:
#!/bin/sh

echo -e $@ 

IFS=$'\n' 
while read line; do
    echo $line >&3
    if [ "${line::2}" = "OK" ] || [ "${line::5}" = "ERROR" ] ; then
        break
    fi
done
  1. 然后我的主脚本中有一个函数:
smsio() {
    response=$(3>&1 socat -T 2 exec:"./atread.sh ''" tcp:xxx.xxx.xxx.xxx:5510,crlf)
    ... (some other code to check for a timeout or broken pipe)
}
  1. 我像这样调用主脚本中的函数:smsio "AT+CMGF=1".

只要参数不包含特殊字符,这种方法就非常有效,特别是:

我尝试使用反斜杠 (\") and/or 添加双引号 ("".."") 来转义特殊字符,但似乎 socat 在调用执行脚本。 对如何通过 socat 获取这些特殊字符有什么建议吗?

PS:我通过字符替换暂时绕过了这个问题(即在参数字符串中使用 ! for ",然后在 atread.sh 中使用 tr '!' '"',但我寻找更优雅的解决方案。

Socats 地址解析器专门解释了一些字符,例如:

: , ! " ' \ ( [ {

你必须用反斜杠转义它们。请注意,您还需要从 shell 解析器中转义一些特殊字符,包括 \。

使用选项-d -d -d -d查看socat接收和使用的参数;试试像

socat -u /tmp/nonexistingdir/x\\x -

在socat执行最后的系统调用时,在错误信息中查看特殊字符剩下的是什么。