LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 10:15 AM   #1
Almaz
Member
 
Registered: Jul 2013
Posts: 35

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, 10:40 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

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, 10:41 AM   #3
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
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 10:41 AM. Reason: I see I was a minute late.
 
1 members found this post helpful.
Old 07-29-2013, 10:55 AM   #4
Almaz
Member
 
Registered: Jul 2013
Posts: 35

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, 11:05 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 11:09 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 11:17 AM   #7
Almaz
Member
 
Registered: Jul 2013
Posts: 35

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, 11:22 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 11:28 AM   #9
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
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, 11:33 AM   #10
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

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, 11:35 AM   #11
Almaz
Member
 
Registered: Jul 2013
Posts: 35

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, 11:37 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 11:40 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 11:42 AM   #14
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

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, 11:49 AM   #15
Almaz
Member
 
Registered: Jul 2013
Posts: 35

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 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 Aquarius_Girl 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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration