August 10, 2018

Matching Strings In Variables – Fish Shell

I’ve recently been completing some boring remediation work of changing some VM folder locations, which required me to change hundreds of tags, so I thought it would be worth seeing if Fish could help me out.

I already knew that the file contents had the something similar to:

default['vmfolder'] = "oldfolder"

I started out by creating an array of files that were owned by my team:

for i in (grep -r "myteam" * | cut -d ':' -f1 | sort | uniq)
     set servers (string join " " $servers $i)

This gave me:

ENV/server1.rb ENV/server2.rb ENV2/server1.rb ENV3/server1.rb ENV3/server2.rb

My main issue was that there were lots of different sites and they’d all need different tags for their new VM locations, but due to us having an array available of our files we could now loop through them and match the start of the environment to fix their locations.

for i in $servers                                                                                                                                                                                                             
     switch $i
         case "*ENV*"
             sed -i 's/oldfolder/ENV_TEAMNAME/' $i
         case "*ENV2*"
             sed -i 's/oldfolder/ENV2_TEAMNAME/' $i
         case "*ENV3*"
             sed -i 's/oldfolder/ENV3_TEAMNAME/' $i

With that done I just had to push the contents back to our repo and I was done. Hope that helps someone else in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *