ash scripting: space-containing variable refuses to be grepped

Posted by Luci Sandor on Super User See other posts from Super User or by Luci Sandor
Published on 2012-12-02T22:37:22Z Indexed on 2012/12/02 23:12 UTC
Read the original article Hit count: 457

Filed under:
|
|
|

I am trying to run the script listed at http://talk.maemo.org/showthread.php?t=70866&page=2 on its intended hardware, a Nokia Linux phone running BusyBox ash. The script receives the name of WiFi network as a parameter, and tries to connect the phone to it. I suspect the script works, but my SSID, BU (802.1x), has space and parentheses in it. So when I type at the command prompt

autoconnect.sh BU\ \(802.1x\)

I get various errors. First,

LIST=`iwconfig wlan0 | awk -F":" '/ESSID/{print $2}'`
if [ $LIST = "\"$1\"" ]; then

...fails, even I am connected to the network. The error is not avoided by using single or double quotes instead of escaping characters at the command prompt.

Second,

if [ -z `iwlist wlan0 scan | grep -m 1 -o \"$1\"` ]; then
    echo SSID \"$1\" not found;

shows that grep does not find the string, although the same grep, typed directly into the command prompt, does find 'BU (802.1x)'.

How do I quote $1 in the two circumstances above so that it will work with my network SSID, containing spaces and parentheses?

Thank you.

© Super User or respective owner

Related posts about grep

Related posts about awk