Results 1 to 6 of 6

Thread: While loop executing even though variables are equal

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question While loop executing even though variables are equal

    Not sure why I can't get this while loop to work. Even when the condition(wifi network is MyWifi) the while loop continues to execute. I am a n00b. What am I doing wrong?

    Code:
    AIRPORT="en0"; #may be en0, use networksetup -listallhardwareports to check
    WIFI= /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}';
    
    
    echo $WIFI
    while [  '$WIFI' != "MyWifi" ];
    do
        echo $WIFI
        networksetup -setairportpower $AIRPORT off
        networksetup -setairportpower $AIRPORT on
        WIFI= networksetup -getairportnetwork en0
        sleep 10
        echo $WIFI
    done
    Last edited by nixcraft; 24th January 2013 at 05:30 AM.

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,513
    Thanks
    17
    Thanked 804 Times in 511 Posts
    Rep Power
    10

    Default

    Change from:
    Code:
    while [  '$WIFI' != "MyWifi" ];
    To (note double quotes around $WIFI):
    Code:
    while [ "$WIFI" != "MyWifi" ];
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by nixcraft View Post
    Change from:
    Code:
    while [  '$WIFI' != "MyWifi" ];
    To (note double quotes around $WIFI):
    Code:
    while [ "$WIFI" != "MyWifi" ];
    No change. Any other thoughts, anyone?

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    447
    Thanks
    0
    Thanked 61 Times in 57 Posts
    Rep Power
    10

    Default

    Code:
    AIRPORT="en0"; #may be en0, use networksetup -listallhardwareports to check
    WIFI=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
    # no spaces around equal sign
    # command substitution needed
    
    echo "$WIFI"
    
    while test "$WIFI" != "MyWifi"
    do
        echo "$WIFI"
        networksetup -setairportpower $AIRPORT off
        networksetup -setairportpower $AIRPORT on
        WIFI=$(networksetup -getairportnetwork en0)
        sleep 10
        echo $WIFI
    done
    Last edited by Watael; 24th January 2013 at 07:52 AM.
    A problem clearly stated is a problem half solved.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Watael View Post
    Code:
    AIRPORT="en0"; #may be en0, use networksetup -listallhardwareports to check
    WIFI=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
    # no spaces around equal sign
    # command substitution needed
    
    echo "$WIFI"
    
    while test "$WIFI" != "MyWifi"
    do
        echo "$WIFI"
        networksetup -setairportpower $AIRPORT off
        networksetup -setairportpower $AIRPORT on
        WIFI=$(networksetup -getairportnetwork en0)
        sleep 10
        echo $WIFI
    done
    AWWWWWWWWWWWWWW YEAAAAAAAAAAAAAAHHHH!!! You got me working! Thanks much!

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Default

    Note that Single quotes will literally interpret the contents, whereas Double quotes will interpret variables or other special symbols inside.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. set parameter to a shell script when executing ???
    By palsec in forum Linux software
    Replies: 0
    Last Post: 26th October 2011, 03:26 AM
  2. Replies: 2
    Last Post: 5th September 2010, 11:50 PM
  3. Problem executing script through at
    By viktor4124 in forum Shell scripting
    Replies: 1
    Last Post: 7th April 2009, 12:09 AM
  4. Executing .exe files in Red Hat Linux AS4
    By shilpigoel1 in forum Linux software
    Replies: 2
    Last Post: 9th December 2008, 07:34 PM
  5. cgi script downloads - instead of executing
    By sathiya in forum Ubuntu / Debian
    Replies: 2
    Last Post: 13th June 2008, 01:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •