Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
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
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
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.
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.