I'd go with something like this, using sourcing. Each server cfg is in a separate file, script finds all cfgs and creates convenient menu for bonus points.
cfg.sh:
Code:
#!/bin/bash
cfg_files=( *.cfg ) # create array of all *.cfg files
select h in "${cfg_files[@]%.cfg}"; do break; done # menu consists of cfg names with extension stripped
source "$h.cfg" # load variables from selected cfg file
echo "${user}:${password}@${host}"
server1.cfg
Code:
host=heaven
user=god
password=amen
server2.cfg
Code:
host=rome
user=caesar
password=ave
how it works
Code:
$ ./cfg.sh
1) server1
2) server2
#? 1
god:amen@heaven
$ ./cfg.sh
1) server1
2) server2
#? 2
caesar:ave@rome
and if you want to stay with a single flat file that looks like this
Code:
heaven god amen
rome caesar ave
Code:
$ read h; read -r host user pw < <( grep "$h" servers.txt ); echo "$host / $user / $pw"
rome
rome / caesar / ave
Bookmarks