0

I'm going to try to describe my problem and my end goal as best as I can, here it goes:

I have a script that fetches AWS ELB information (elb name + ports that's associated with a specific certifican arn).

So, in the end I have a text file (I call it elb_ports) and it looks something like this:

ccds-lb 636 
cf-router 443 4443 
dev-cf-router 443 4443 
eng-jenkins-monit 443 
gitlab-lb 443 
gitlab-mattermost-elb 443 
jenkins-np-elb 443 
saml 443  
uaa 443 

I have another script that comes after that which I want it to go through that elb_ports file and replace the certificates with a new one, but according to Amazon's documentation: It says in order to replace the certificates, I need two things from that elb_ports file. The load balancer name and the load balancer port.

So basically their command looks like this

aws elb set-load-balancer-listener-ssl-certificate \
  --load-balancer-name my-load-balancer \
  --load-balancer-port 443 \
  --ssl-certificate-id arn:aws:iam::123456789012:server-certificate/my-new-certificate

I want to be able to loop through the file and execute the command above to each elb and port, but my problem is with the elbs that has multiple ports associated with the cert like: cf-router 443 4443 for example.

So my idea was to split that into two lines, so like this:

cf-router 443
cf-router 4443

But I'm not sure how to add cf-router (for example) to the ports that come after the first one (there could be more than two ports using the same cert).

I hope I was able to explain my problem and end goal clearly, if this isn't a good method, I'm open to suggestions also.

EDIT: Perhaps something like this is beneficial, but not sure how to tailor it to my needs.. Like put each line in an array and the space as a delimiter and then loop through each line putting arr(1) (load balancer name) and then the load balancer port, but not sure how to count and go through >arr(2) in bash.

Community
  • 1
  • 1
Fadi
  • 1,329
  • 7
  • 22
  • 40
  • Are you ok with a Python solution? – helloV Mar 21 '17 at 19:36
  • Unfortunately it has to be bash, otherwise it's much easier to do lol. – Fadi Mar 21 '17 at 19:38
  • Well, I'm not sure how to add cf-router (for example) to the ports that come after the first one (there could be more than two ports using the same cert), if I want to go that route.. But also check my EDIT to the question.. – Fadi Mar 21 '17 at 19:44
  • Yes, it could potentially have more than two ports. – Fadi Mar 21 '17 at 19:45
  • I'm trying to figure out what you actually want, but in reading this question I keep getting bogged down in details about why you want it, not what "it" actually is. If all you're asking for is a way to split out lines of more than two columns into two-column pairs, that's trivial, but I'm entirely unclear as to whether or not it answers your question. – Charles Duffy Mar 21 '17 at 19:47

1 Answers1

1

To split out your extra columns into separate lines:

while read -r lb_name lb_ports_str; do   ## split line into lb name and port list string
  read -r -a lb_ports <<<"$lb_ports_str" ## split out port list string into an array
  for port in "${lb_ports[@]}"; do       ## iterate through that array
    printf '%s %s\n' "$lb_name" "$port"  ## handle each port separately
  done
done <elb_ports                          ## reading lines from elb_ports

Of course, that printf could be any other line referring to $lb_name and $port -- meaning you could potentially run your code that's installing new certificates here.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Sorry for the dumb question but does this overwrite the file or just print out to console? Because it seems to be doing the latter for me. – Fadi Mar 21 '17 at 19:58
  • I'd probably have to do `printf '%s %s\n' "$name" "$port" >> new_file` right? – Fadi Mar 21 '17 at 19:59
  • Hmm, when I did that it only printed the ports into a new file, no elb names. :( – Fadi Mar 21 '17 at 20:01
  • Oops, I had `lb_name` in one place and `name` in the other; fixed. – Charles Duffy Mar 21 '17 at 20:01
  • And you're better off putting `>new_file` after the `done`, to only open the output file once for the whole loop rather than re-opening it once per line. – Charles Duffy Mar 21 '17 at 20:02
  • Ah! That works great! Thank you @Charles Duffy, you make it look easy.. :P – Fadi Mar 21 '17 at 20:04