LinuxQuestions.org
Visit Jeremy's Blog.
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, 11:15 AM   #1
Almaz
Member
 
Registered: Jul 2013
Posts: 31

Rep: Reputation: Disabled
If Statement


I'm having a problem to use a simple if statement and I need a little directions. Already tried different ways but can't get it to work. Thanks in advance

mywhome=$(wl ssid | awk '{print $3}')
echo $mywhome ----> Returns "1homessid2"
if [ "$mywhome" == *homessid* ] ---> THAT'S A PROBLEM, PLEASE HELP
then
echo 1
else
echo 2
fi
 
Old 07-29-2013, 11:40 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,013

Rep: Reputation: Disabled
Quote:
Originally Posted by Almaz View Post
if [ "$mywhome" == *homessid* ] ---> THAT'S A PROBLEM, PLEASE HELP
It should be either
Code:
if [ "$mywhome" = *homessid* ]
or
Code:
if [[ "$mywhome" == *homessid* ]]
 
Old 07-29-2013, 11:41 AM   #3
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 569
Blog Entries: 3

Rep: Reputation: 73
You must double the brackets.

Code:
mywhome=$(wl ssid | awk '{print $3}') 
echo $mywhome
if [[ "$mywhome" == *homessid* ]]
then
echo 1
else
echo 2
fi

Last edited by goumba; 07-29-2013 at 11:41 AM. Reason: I see I was a minute late.
 
1 members found this post helpful.
Old 07-29-2013, 11:55 AM   #4
Almaz
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thanks Goumba and Ser Olmy for your advice but I already tried these methods before. None of them works. I've already spent today over 2 hours on a simple line and I still can't get it to work. Any other suggestions?
 
Old 07-29-2013, 12:05 PM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 843
Blog Entries: 2

Rep: Reputation: 212Reputation: 212Reputation: 212
You have not described in precise English what you intend to happen. I suspect you want a case statement.

Code:
case "$mywhome" in
*homessid*) echo 1;;
*) echo 2;;
esac
 
Old 07-29-2013, 12:09 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
This seems to work fine here
Code:
if [[ "$mywhome" = *homessid* ]];
then
    echo 1
else
    echo 2
fi
are you just missing the ;

alternative
Code:
$(echo $mywhome | grep -q homessid ) && echo 1 || echo 2
or even
Code:
wl ssid | grep -q homessid && echo 1 || echo 2
depending on what you are doing, you might find case more useful

Code:
mywhome=$(wl ssid | awk '{print $3}')
case $myhome in
    1homessid2) .. some actions;;
    2homessid1) .. some different actions;;
    randomSSID) .. some very different actions;;
             *) .. action for no match;;
esac
 
1 members found this post helpful.
Old 07-29-2013, 12:17 PM   #7
Almaz
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
You have not described in precise English what you intend to happen. I suspect you want a case statement.

Code:
case "$mywhome" in
*homessid*) echo 1;;
*) echo 2;;
esac
Well, I'm just doing a simple toggle script to rejoin from one network to another network. If & Else command should work just fine. Since I've already spent so much time on a single command line, I want to know why such a simple if command doesn't work. I'm sure something basic is missing. Here is my full script should look like, if it makes any difference.

mywhome=$(wl ssid | awk '{print $3}');
if [[ $mywhome =~ *ssai-i* ]]
then
led white off
led amber on
nvram set wl0_ssid=Wssai-iD
nvram set wl0_wpa_psk=11111111
service wl restart
service wan restart
else
led amber off
led white on
nvram set wl0_ssid=BssiD
nvram set wl0_wpa_psk=22222222
service wl restart
service wan restart
fi
 
Old 07-29-2013, 12:22 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
mywhome=$(wl ssid | awk '{print $3}');
if [[ $mywhome =~ *ssai-i* ]];
then
    led white off
    led amber on
    nvram set wl0_ssid=Wssai-iD
    nvram set wl0_wpa_psk=11111111
    service wl restart
    service wan restart
else
   led amber off
   led white on
   nvram set wl0_ssid=BssiD
   nvram set wl0_wpa_psk=22222222
   service wl restart
   service wan restart
fi
paste code in [code][/code]
Code:
it will
    preserve
           indents
and       such
 
Old 07-29-2013, 12:28 PM   #9
goumba
Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Wheezy (7.0), Jessie (future 8) and Sid (unstable)
Posts: 569
Blog Entries: 3

Rep: Reputation: 73
Quote:
Originally Posted by Firerat View Post
This seems to work fine here
...
are you just missing the ;
The semicolon is required if the entire statement was on a singe line. In the case of the posted script and following, it is not required.

Code:
if [ ... ]
then
...
fi
and

Code:
if [ ... ]; then ...; fi
 
1 members found this post helpful.
Old 07-29-2013, 12:33 PM   #10
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,013

Rep: Reputation: Disabled
Quote:
Originally Posted by Almaz View Post
if [[ $mywhome =~ *ssai-i* ]]
*ssai-i* is not a valid regexp.

Or, it may be semi-valid, but "any number of occurences of nothing, followed by ssai-, followed by any number of i's" is probably not what you were aiming for.

Try .*ssai-i.* instead.
 
Old 07-29-2013, 12:35 PM   #11
Almaz
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Ok guys, can anybody get the output of this script to 1 instead of 2

mywhome=$(wl ssid | awk '{print $3}')
echo $mywhome ----> Returns "Test-Ssid"
if [[ "$mywhome" == *est-Ssi* ]] ---> THAT'S A PROBLEM, PLEASE HELP
then
echo 1
else
echo 2
fi


Always shows up as output 2.
 
Old 07-29-2013, 12:37 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by goumba View Post
The semicolon is required if the entire statement was on a singe line. In the case of the posted script and following, it is not required.
oops,
not sure why but I have always done it like that

in that case, I have no idea why it is not working
 
Old 07-29-2013, 12:40 PM   #13
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
Ok guys, can anybody get the output of this script to 1 instead of 2
>


Always shows up as output 2.
see Ser Olmy's post
 
Old 07-29-2013, 12:42 PM   #14
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,013

Rep: Reputation: Disabled
Quote:
Originally Posted by Almaz View Post
if [[ "$mywhome" == *est-Ssi* ]] ---> THAT'S A PROBLEM, PLEASE HELP
You can't match against a string using shell wildcards. Bash will expand *est-Ssi* to the names of any matching file names in the current directory, or leave it as is if no matching names exist. In either case, it isn't going to work.

Either match against a literal string, or use regexp matching:
Code:
if [[ "$mywhome" =~ .*est-Ssi.* ]]
(And as Firerat pointed out, do use [code] tags like I just did.)
 
Old 07-29-2013, 12:49 PM   #15
Almaz
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ser Olmy View Post
Either match against a literal string, or use regexp matching:
Code:
if [[ "$mywhome" =~ .*est-Ssi.* ]]
(And as Firerat pointed out, do use [code] tags like I just did.)
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.
 
  


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 06:16 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 01:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 09:05 PM


All times are GMT -5. The time now is 05:55 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