I have a RNDIS USB modem that allows AT commands through a telnet port (5510). I want to use it to read and send SMS messages from a script. I've used a variant of a similar question posted here but I'm using socat instead of netcat.
- I have a helper script called 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
- I then have a function in my main script:
smsio() {
response=$(3>&1 socat -T 2 exec:"./atread.sh '$1'" tcp:xxx.xxx.xxx.xxx:5510,crlf)
... (some other code to check for a timeout or broken pipe)
}
- I invoke the function in the main script like so:
smsio "AT+CMGF=1"
.
This works extremely well as long as the parameter does not include special characters, specifically:
- Quotation marks (") as in
smsio 'AT+CMGL="ALL"'
to read all SMS messages - Backslash (\) as in
smsio 'AT+CMGS="123456789"' ; smsio "Text Message\x1A"
to send an SMS
I've tried to escape the special character using backslash (\") and/or adding double quoting ("".."") but it appears that socat is stripping all special characters before it calls the exec script. Any suggestions how to get these special characters through socat?
PS: I've temporarily bypassed the problem via character substitution (i.e using ! for " in the parameter string, then using tr '!' '"'
in atread.sh, but I'm looking for a more elegant solution.