Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The output of iwconfig puts the name of the access point in double quotes, which leads to some weirdness. The author of the script deals with this "artifact" in his script rather than getting rid of it right away. I suggest to remove the quotes before doing anything else. Two possible approaches would be:

$ eval moo=`iwconfig wlan0 | awk -F":" '/ESSID/{print $2}' `
$ moo=`iwconfig wlan0 | awk -F":" '/ESSID/{print $2}' |tr -d \"`

The first one executes the variable assignment statement "literally", while the other one removes double quotes from the output string.

The second problem with the script lies in the assumption that it is fine to use $LIST without quoting it (since the string would have contained double quotes). I think this is a mistake because the string still evaluates as two tokens:

$ moo='"aaa bbb"'
$ if [ $moo = "\"aaa bbb\""];then ok;fi
ash: bbb": unknown operand

$  if [ "$moo" = "\"aaa bbb\"" ];then echo ok;fi
ok

In order to fix this, quote the parameters in the square brackets and get rid of the escaped quotes in the second argument (because we removed them from the iwconfig output):

if [ "$LIST" = "$1" ]; then ...

Also, in order to pass a string with irregular character (space, parenthesis) as one argument to a script, just quote it:

$ autoconnect.sh "a name (with a comment)"

The last part about matching the name with grep has already been adequately answered by glenn jackman.

Note: The above was tested with BusyBox ash (1.20.2).

share|improve this answer
    
Thank you, I wish I could mark both answers as accepted. Both worked great, or at least it seems so. Alas, the script never succeeds, because the next line is an awk that requires each space to be escaped. Will post it as a separate question. –  Nick Sandor Dec 8 '12 at 4:21

Don't try to add literal quotes. You need if [ "$LIST" = "$1" ]; .... Both variables need to be quoted so the test command is given exactly 3 arguments -- this is crucial particular because the values contain spaces.

Same advice for the second: if [ -z $(iwlist wlan0 scan | grep -m 1 -o "$1") ]; ...

The better way to write the above: if iwlist wlan0 scan | grep -q "$1"; ...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.