<div><div dir="auto">This really would be a lot simpler with Ansible and local actions. </div><br><div class="gmail_quote"><div>On Sun, Jun 25, 2017 at 21:48 askabt <<a href="mailto:askabt@gmail.com">askabt@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Trying to write a script in Bash to generate an ssh config file from a<br>
list. The list will grow and shrink and the script will regenerate ssh's<br>
config file.<br>
<br>
I know there is a better way to do this and probably this isn't the best<br>
approach. But still was trying to do it with bash, just to figure it<br>
out if it can be done. Maybe someone knows where I need to look to<br>
figure it out, or maybe it's not possible. I'm fine either way.<br>
<br>
This script builds several arrays from one array(file) to call them<br>
later in a loop. Will have to create a ./ssh_config file with variables<br>
to run script. Or generate your own arrays, I kept it short.<br>
<br>
build_config () ; Works in theory but doesn't increment.<br>
<br>
build_fail () ; Well it fails.<br>
<br>
Running the build_fail Function will error out with<br>
<br>
script: line 48: Host ${aaray$[f][0]}: bad substitution<br>
<br>
or<br>
<br>
script: line 48: Host ${aaray$f[0]}: bad substitution<br>
<br>
<br>
#!/bin/bash<br>
<br>
# Example file format for the ./ssh_config file, remove hash in file:<br>
#{Host} {Hostname}<br>
#bastion <a href="http://b.server.com" rel="noreferrer" target="_blank">b.server.com</a><br>
#mail <a href="http://mail.server.com" rel="noreferrer" target="_blank">mail.server.com</a><br>
#mail2 <a href="http://mail2.server.com" rel="noreferrer" target="_blank">mail2.server.com</a><br>
#dns <a href="http://dns.server.com" rel="noreferrer" target="_blank">dns.server.com</a><br>
<br>
# Establish Variables:<br>
IFS=' '<br>
readarray -t list < ./ssh_config<br>
<br>
<br>
# Builds a set of arrays in loop $aaray{1..?}<br>
# Loop to create individual aarays, range 1 through ${!list[@]}<br>
for i in ${!list[@]}<br>
do<br>
<br>
declare -a aaray${i}<br>
declare "aaray${i}=( ${list[${i}]} )"<br>
<br>
done<br>
<br>
<br>
#<br>
# Loop to build config file. Works but does not increment.<br>
#<br>
build_config () {<br>
<br>
for i in ${!list[@]}; do<br>
<br>
echo "Host ${aaray1[0]}"<br>
echo " Hostname ${aaray1[1]}"<br>
echo<br>
<br>
done<br>
}<br>
<br>
#<br>
# Loop to increment $aaray[number] by on on each pass. Fails.<br>
#<br>
build_fail ()<br>
{<br>
<br>
for f in ${!list[@]}; do<br>
<br>
echo "Host ${aaray$[f][0]}"<br>
echo " Hostname ${aaray$[f][1]}"<br>
echo<br>
<br>
done<br>
}<br>
<br>
# Toggle functions for testing<br>
<br>
#build_config<br>
build_fail<br>
<br>
### EOF ###<br>
<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org" target="_blank">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</blockquote></div></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature">James Sumners<br><a href="http://james.sumners.info/" target="_blank">http://james.sumners.info/</a> (technical profile)<br><a href="http://jrfom.com/" target="_blank">http://jrfom.com/</a> (personal site)<br><a href="http://haplo.bandcamp.com/" target="_blank">http://haplo.bandcamp.com/</a> (music)</div>