When scripting in bash under Oracle Linux and in need to search and replace strings in a text file the sed command is where most people turn to. reason for this is that sed is a stream editor for filtering and transforming text and makes it ideal for this purpose.
I recently started developing a full end to end automation and integration for supporting projects within our company to work with the Oracle public cloud. One of the options to do automation with the Oracle cloud is using Terraform. Terraform enables you to safely and predictably create, change, and improve production infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned. Which means, Terraform helps you to make full use of infrastructure as code when working with the Oracle Cloud.
One of the simple bugs in my code I encountered was “strange” error somewhere in the functions developed to create a Terraform plan. In effect the Terraform base plan we developed was a plan without any specifics. One of the specifics needed was the API endpoint of the Oracle Public Cloud which needed to be changed from a placeholder into the real value when provided by a Jenkins build job.
The initial unit testing was without any issue while using random values, however, every time a valid URL format was used the code would break and the Jenkins build responsible for building the Terraform plan for the Oracle cloud would end up as a broken build.
The error message received was the following:
sed: -e expression #1, char 26: unknown option to `s'
Reason for this was the original construction of the sed command used in the code. Orignally used was the below sed command to replace the ##OPC_ENDPOINT## with the actual API endpoint for the Oracle public cloud.
sed -i -e "s/##OPC_ENDPOINT##/$endpoint/g" terraformplan.tf
Due to the way we use / in this command we have an issue if we populate the $endpoint with a URL which also contains a / character. The fix is rather simple, when you know it. if you use sed to work with URL’s you should use a , and not /. Meaning, your code should look like the one below to do a valid replace with sed
sed -i -e "s,##OPC_ENDPOINT##,$endpoint,g" terraformplan.tf