2

I am presently in the process of writing a Bash function that when invoked will ask users to choose from a list of Sabayon's mirrors, but I would like it if each option could include info on the mirror in question, like its country and connection speed. Currently I have this Bash function:

function sabayon-mirror {
  L=('ftp://mirrors.coopvgg.com.ar/sabayon/ 
  ftp://gd.tuwien.ac.at/linux/sabayonlinux/
  ftp://mirror.optusnet.com.au/sabayon')
  select x in "${L[@]}"
  do
    export MIRROR="$x"
  done
}

Adding the mirror info after each URL makes the info be treated like another option to be selected from.

EDIT: Chaos' answer was what I was originally looking for, but if you have an answer that doesn't require me to list every mirror manually (but rather can fetch it from a remote list) I would still love to hear about it.

Josh Pinto
  • 3,483
  • 15
  • 52
  • 87
  • how do you propose to get the information you want to write out? or are you asking how? if the latter, it might be a good idea to ask in a sabayon forum. – mikeserv Nov 25 '15 at 12:00
  • The info about each mirror I'm willing to provide to the script (like manually writing it in for each mirror I list), I'm just asking how might I show the info on each mirror in the select interface. – Josh Pinto Nov 25 '15 at 12:06
  • oh gee. are you sure? dunno about sabayon, but it seems to me that ought to be available to you. on an arch system it is in `/etc/pacman.d/mirrors.`*`forgot extension`*. but i use the [`json`](https://www.archlinux.org/mirrors/status/json/) or [url query](https://www.archlinux.org/mirrorlist/?country=US&protocol=https&use_mirror_status=on) interfaces. all of the distros do stuff like that - mirror status is a big deal to the distro maintainers. there's gotta be something like that you can do - live. – mikeserv Nov 25 '15 at 12:13
  • Thanks, ya gave me an idea something like that is available for Sabayon, but nonetheless I'd like this answer in Bash script as I am working on other Bash scripts that will use the `$MIRROR` environment variable. – Josh Pinto Nov 25 '15 at 12:15
  • well, you're already connecting in the bash script as it is. im not suggesting you switch languages, just that you make another query. oh, you're not. but you *should*. – mikeserv Nov 25 '15 at 12:17

2 Answers2

3

How about that:

function sabayon-mirror {
  L=('Info about mirror 1 - ftp://mirrors.coopvgg.com.ar/sabayon/' 
  'Info about mirror 2 - ftp://gd.tuwien.ac.at/linux/sabayonlinux/'
  'Info about mirror 3 - ftp://mirror.optusnet.com.au/sabayon')
  select x in "${L[@]}"
  do
    export MIRROR="${x#*-}"
  done
}

The Info is part of the item. But, when exporting the variable value, it is removed.

chaos
  • 47,463
  • 11
  • 118
  • 144
2

You are defining an array with a single entry in your script. SInce you are single quoting all elements, they will be treated as a single, long string. Array elements in bash are separated by spaces so you need to quote each separately:

L=('ftp://mirrors.coopvgg.com.ar/sabayon/' 
  'ftp://gd.tuwien.ac.at/linux/sabayonlinux/'
  'ftp://mirror.optusnet.com.au/sabayon')

That said, since you are using bash, you can use associative arrays:

function sabayon-mirror {
  declare -A L=(
    [Info about mirror 1]="ftp://mirrors.coopvgg.com.ar/sabayon/"
    [Info about mirror 2]="ftp://gd.tuwien.ac.at/linux/sabayonlinux/"
    [Info about mirror 3]="ftp://mirror.optusnet.com.au/sabayon")
  select x in "${!L[@]}"
  do
    export MIRROR="${L[$x]}"
  done
}

With that in mind, I took the list of Sabayon mirrors from here and wrote the function for you:

## Declare the associative array
declare -A L=(
[Argentina 1]="ftp://mirrors.coopvgg.com.ar/sabayon/entropy"              [Argentina 2]="http://mirrors.coopvgg.com.ar/sabayon/entropy"
[Austria 1]="ftp://gd.tuwien.ac.at/linux/sabayonlinux/entropy"            [Austria 2]="http://gd.tuwien.ac.at/linux/sabayonlinux/entropy"
[Australia 1]="ftp://mirror.internode.on.net/pub/sabayon/entropy"         [Australia 2]="http://mirror.internode.on.net/pub/sabayon/entropy"
[Australia 3]="ftp://mirror.optusnet.com.au/sabayon/entropy"              [Australia 4]="http://mirror.optusnet.com.au/sabayon/entropy"
[Belgium 1]="ftp://ftp.belnet.be/mirror/sabayonlinux/entropy"             [Belgium 2]="http://ftp.belnet.be/mirror/sabayonlinux/entropy"
[Brazil 1]="ftp://sabayon.c3sl.ufpr.br/sabayon/entropy"                   [Brazil 2]="http://sabayon.c3sl.ufpr.br/entropy"
[Czech Republic 1]="ftp://mirror.dkm.cz/pub/sabayon/entropy"              [Czech Republic 2]="http://sabayon.mirror.dkm.cz/pub/sabayon/entropy"
[Germany 1]="http://mirror.de.sabayon.org/entropy"                        [Denmark 1]="ftp://ftp.klid.dk/sabayonlinux/enttropy"
[Denmark 2]="http://ftp.klid.dk/sabayonlinux/entropy"                     [Greece 1]="ftp://ftp.cc.uoc.gr/mirrors/linux/SabayonLinux/entropy"
[Greece 2]="http://ftp.cc.uoc.gr/mirrors/linux/SabayonLinux/entropy"      [Hungary 1]="ftp://ftp.fsn.hu/pub/linux/distributions/sabayon/entropy"
[Hungary 2]="http://ftp.fsn.hu/pub/linux/distributions/sabayon/entropy"   [Italy 1]="http://mirror.it.sabayon.org/entropy"
[Italy 2]="ftp://na.mirror.garr.it/mirrors/sabayonlinux/entropy"          [Italy 3]="http://na.mirror.garr.it/mirrors/sabayonlinux/entropy"
[Japan 1]="ftp://ftp.riken.jp/Linux/sabayon/entropy"                      [Japan 2]="http://ftp.riken.jp/Linux/sabayon/entropy"
[Japan 3]="ftp://ftp.kddilabs.jp/Linux/packages/sabayonlinux/entropy"     [Japan 4]="http://ftp.kddilabs.jp/Linux/packages/sabayonlinux/entropy"
[Japan 5]="http://ftp.tsukuba.wide.ad.jp/Linux/sabayon/entropy"           [Netherlands 1]="ftp://ftp.nluug.nl/pub/os/Linux/distr/sabayonlinux/entropy"
[Netherlands 2]="http://ftp.nluug.nl/os/Linux/distr/sabayonlinux/entropy" [Netherlands 3]="ftp://ftp.surfnet.nl/pub/os/Linux/distr/sabayonlinux/entropy"
[Portugal 1]="ftp://glua.ua.pt/sabayon/entropy"                           [Portugal 2]="ftp://ftp.rnl.ist.utl.pt/pub/sabayon/entropy"
[Portugal 3]="http://ftp.rnl.ist.utl.pt/pub/sabayon/entropy"              [Russian Federation 1]="ftp://mirror.yandex.ru/sabayon/entropy"
[Russian Federation 2]="http://mirror.yandex.ru/sabayon/entropy"          [Sweden 1]="ftp://ftp.portlane.com/pub/os/linux/sabayon/entropy"
[Sweden 2]="http://ftp.portlane.com/pub/os/linux/sabayon/entropy"         [United States 1]="ftp://mirrors-usa.go-parts.com/sabayon/entropy"
[United States 2]="http://mirrors-usa.go-parts.com/sabayon/entropy"       [United States 3]="ftp://mirror.cs.vt.edu/pub/SabayonLinux/entropy"
[United States 4]="http://mirror.cs.vt.edu/pub/SabayonLinux/entropy"      [United States 5]="http://cross-lfs.sabayonlinux.org/entropy"
[United States 6]="http://mirror.clarkson.edu/sabayon/entropy"            [United States 7]="http://mirror.umd.edu/sabayonlinux/entropy"
[South Africa 1]="ftp://sabayon.mirror.ac.za/entropy"                     [South Africa 2]="http://sabayon.mirror.ac.za/entropy"
)
## Unfortunately, associative arrays are not stored in the
## order you create them with so, to have the select show
## sorted options, we need a second, helper array.
sorted=(
[1]="Argentina 1"             [2]="Argentina 2"             [3]="Australia 1"
[4]="Australia 2"             [5]="Australia 3"             [6]="Australia 4"
[7]="Austria 1"               [8]="Austria 2"               [9]="Belgium 1"
[10]="Belgium 2"              [11]="Brazil 1"               [12]="Brazil 2"
[13]="Czech Republic 1"       [14]="Czech Republic 2"       [15]="Denmark 1"
[16]="Denmark 2"              [17]="Germany 1"              [18]="Greece 1"
[19]="Greece 2"               [20]="Hungary 1"              [21]="Hungary 2"
[22]="Italy 1"                [23]="Italy 2"                [24]="Italy 3"
[25]="Japan 1"                [26]="Japan 2"                [27]="Japan 3"
[28]="Japan 4"                [29]="Japan 5"                [30]="Netherlands 1"
[31]="Netherlands 2"          [32]="Netherlands 3"          [33]="Portugal 1"
[34]="Portugal 2"             [35]="Portugal 3"             [36]="Russian Federation 1"
[37]="Russian Federation 2"   [38]="South Africa 1"         [39]="South Africa 2"
[40]="Sweden 1"               [41]="Sweden 2"               [42]="United States 1"
[43]="United States 2"        [44]="United States 3"        [45]="United States 4"
[46]="United States 5"        [47]="United States 6"        [48]="United States 7"
)
select x in "${sorted[@]}"
do
    export MIRROR="${L[$x]}"
    break
done

There's bound to be a better way to do this, I'm sure the available mirrors are stored somewhere in your Sabayon system, but I don't use Sabayon so I don't know. According to the page I linked to above, they are probably stored in /etc/entropy/repositories.conf.d. If you edit your question with the format of that directory, we should be able to help you generate the mirror list on the fly.

terdon
  • 234,489
  • 66
  • 447
  • 667