Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How should I construct this?



On Wed, Apr 24, 2013 at 02:16:26PM -0400, TJ Luoma wrote:
> 
> I am trying to write a shell script which will help my computer
> automatically join Wi-Fi networks.
> 
> Each network needs to have an SSID (which may have spaces in it) and
> a password (which may have spaces, punctuation, etc in it).
> 
> I'm trying to figure out the best way to create this.
> 
> I thought about trying to make an array or something like this where
> the first 'column' would be the SSID and the 2nd column would be the
> passwords
> 
> ALL_WIFI_NETWORKS=(
>               Home                    89382ashfa
>               Work                    0823u2j98dyumn
>               "Coffee House"      ""
>               "Jenny's Wifi"      8675309
> )
> 
> 
> but then I need to be able to loop through $ALL_WIFI_NETWORKS using
> only first column… something like this
> 
> for SSID in {{{The First Arg in Each Line of $ALL_WIFI_NETWORKS}}}
> do
>       echo "foo"
> 
> done
> 
> 
> where the part in {{{ and }}} indicates the part where I really
> don't know how to do what I want to do.
> 
> It seems like there's got to be an easier / better way of doing
> this, but I can't figure out what it is, other than keeping two
> lists/arrays, one of the SSIDs, and one with the passwords, but that
> seems kludgey because I have to ask the user (whoever uses this
> script besides me) to put the SSIDs in twice.

Use an associative array:

    typeset -A wifi
    wifi=(
        Home            89382ashfa
        Work            0823u2j98dyumn
        'Coffee House'  ''
        "Jenny's Wiki"  8675309
    )
    for name in ${(k)wifi}; do print "$name -> ${wifi[$name]}"; done

Paul.

-- 
Paul Hoffman <nkuitse@xxxxxxxxxxx>



Messages sorted by: Reverse Date, Date, Thread, Author