LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-29-2013, 12:09 PM   #16
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444

bit confused, as I can't get it to not work

Code:
mywhome=Test-Ssid; if [[ "$mywhome" == *est-Ssi* ]]; then echo 1;else echo 2;fi
mywhome=Test-Ssid; if [[ "$mywhome" == *st-Ssi* ]]; then echo 1;else echo 2;fi
mywhome=Test-Ssid; if [[ "$mywhome" == *Ssi* ]]; then echo 1;else echo 2;fi
touch FooSsiBar
mywhome=Test-Ssid; if [[ "$mywhome" == *Ssi* ]]; then echo 1;else echo 2;fi
all give me 1


But why do you not know the ssid?
It looks like you know it because you set it
nvram set wl0_ssid=Wssai-iD
so I don't understand why you need to use wildcards

and case can do reg. expr. matching as pointed out by linosaurusroot

Your script using case and a function to cut down on duplicate code

Code:
function SwitchWLN () {
led white off
led amber on
nvram set wl0_ssid=$1
nvram set wl0_wpa_psk=$2
service wl restart
service wan restart 
}
mywhome=$(wl ssid | awk '{print $3}')
case mywhome in
    *ssai-i*) SwitchWLN Wssai-iD 11111111;;
           *) SwitchWLN BssiD 22222222;;
esac
But that seems odd to me, Xssai-iD-foo will also match
 
Old 07-29-2013, 12:12 PM   #17
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by Almaz View Post
Just tested your way and got the output
Code:
[[: =~: unknown operand
2
When I get home today I'll try without dash. I'm guessing dash is killing everything.


Code:
dash
mywhome=Test-Ssid; if [[ "$mywhome" == *Ssi* ]]; then echo 1;else echo 2;fi
dash: 1: [[: not found
2
just #!/bin/bash at the top of your script
 
Old 07-29-2013, 01:29 PM   #18
Almaz
LQ Newbie
 
Registered: Jul 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
Sorry for being a pain but even adding #!/bin/sh returns as error. Bash is not installed in Tomato firmware unless I install opt-ware. A quick question $mywhome variable returns with quotation marks such as "Test-Ssid" and that's why I've been using wild cards. Could that be a problem?
 
Old 07-29-2013, 01:39 PM   #19
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 1,999

Rep: Reputation: Disabled
Quote:
Originally Posted by Almaz View Post
Sorry for being a pain but even adding #!/bin/sh returns as error. Bash is not installed in Tomato firmware unless I install opt-ware.
Is grep available? This:
Code:
if echo $mywhome | grep est-Ssi
does the same as
Code:
if [[ "$mywhome" =~ .*est-Ssi.* ]]
in bash.

Last edited by Ser Olmy; 07-29-2013 at 01:40 PM.
 
Old 07-29-2013, 01:46 PM   #20
Almaz
LQ Newbie
 
Registered: Jul 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
Just got back from lunch and got it to work with single quotes I just remembered I had a similar problem in PHP a few years ago so I tried wrapping double quotes inside of single quotes and it worked.

Code:
if [ $mywhome = '"Test-Ssid"' ]

Last edited by Almaz; 07-29-2013 at 01:48 PM.
 
Old 07-29-2013, 01:51 PM   #21
Almaz
LQ Newbie
 
Registered: Jul 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ser Olmy View Post
Is grep available? This:
Code:
if echo $mywhome | grep est-Ssi
does the same as
Code:
if [[ "$mywhome" =~ .*est-Ssi.* ]]
in bash.
GREP works fine since I'm using grep to get SSID in $mywhome
 
Old 07-29-2013, 01:58 PM   #22
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by Almaz View Post
Sorry for being a pain but even adding #!/bin/sh returns as error. Bash is not installed in Tomato firmware unless I install opt-ware. A quick question $mywhome variable returns with quotation marks such as "Test-Ssid" and that's why I've been using wild cards. Could that be a problem?
Ahh, this will go a long way to explain why things are not working as expected.

I've never used Tomato firmware, I have OpenWRT which in principle is the same.

A Little test

Code:
mywhome=Test-Ssid
if [[ "$mywhome" == "*Ssi*" ]]; then echo 1;else echo 2;fi
case $mywhome in *Ssi*) echo 1;;*)echo 2;;esac
my output is:
2
1

This is using busybox's ash, but I get the same result with busybox's sh

I think case is the way forward

However, that does assume you have support for case complied in


ref. quoted "Test-Ssid"
guessing field 3 of wl ssid is "Test-Ssid"
you could either set awk's field separator to " ( -F\" or -F'"'), and adapt the fieldNo. to suit.
or gsub(/\"/,"");print $3 to remove them
 
Old 07-29-2013, 02:13 PM   #23
Almaz
LQ Newbie
 
Registered: Jul 2013
Posts: 29

Original Poster
Rep: Reputation: Disabled
at first I wanted to use AWK but I was lazy to check how to use it, because it's been a while since I used it. I wanted to make it work with wild cards which should be easy as cake but hey, it took me a few hours by trial and errors, and I still couldn't get it work with wild cards. That's what happens when you try to take a shortcut. I'm a big fan of DD-WRT and switched to Tamoto a few month ago and like it as well. Also tried OpenWRT and liked it. I think if DD-WRT or Tomato can do it then there is no point for OpenWRT. When I get a chance I want to play with OpenWRT Dual Wan since it comes with nice GUI. I just don't know if it can automatically auto balance it or I have to setup manually iptables. Anyway thanks to everyone who helped me out.

Last edited by Almaz; 07-29-2013 at 02:14 PM.
 
Old 07-30-2013, 07:30 AM   #24
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 551
Blog Entries: 3

Rep: Reputation: 66
Quote:
Originally Posted by Almaz View Post
When I get home today I'll try without dash. I'm guessing dash is killing everything.
While I didn't get errors, originally I had #!/bin/sh which of course on Debian by default sh is a link to dash. While I did not get an error with my working script, I did get unexpected results, so just something to be aware of.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unless next statement casualzone Linux - Software 1 02-26-2012 05:16 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM


All times are GMT -5. The time now is 04:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration