Reputation: 481
I have a README.md file where I'd like to replace a text identifier {{CODESHIP_CODE}}
with a line of code – specifically a build status image code snippet that includes the git branch name.
I'm thinking it would look something like this...
git rev-parse --abbrev-ref HEAD
in bash variablesed
(possibly grep
) to search the README.md file for the specified {{CODESHIP_CODE}}
pattern/string and replace it with the build image status codeThe code I wrote looks like this:
#!/bin/bash
# Get the current branch
function git_branch {
git rev-parse --abbrev-ref HEAD
}
# Set variable to current branch
branch=$(git_branch)
# Define the pattern to search for
# This pattern/string gets replaced with the build status image code
id="{{CODESHIP_CODE}}"
# Create build status image code
# Inject the $branch variable into the correct location
codeship_build_status="[ ![Codeship Status for ExampleGitHubUser/ExampleRepo](https://codeship.com/projects/a99d9999-9b9f-9999-99aa-999a9a9a9999/status?branch=$branch)](https://codeship.com/projects/999999)"
# Find the pattern to replace
# Then replace it with the build status image
sed -i -e "s/{{CODESHIP_CODE}}/$codeship_build_status/g" README.md
The problem is that I keep getting the following error:
sed: 1: "s/{{CODESHIP_CODE}}/[ ! ...": bad flag in substitute command: 'a'
I'm not sure how to resolve this issue, so that the script works correctly. Any help would be greatly appreciated.
Upvotes: 1
Views: 95
Reputation: 241918
You can't use a replacement string that contains the unescaped delimiter. Escape the delimiters in the string, or switch to different ones.
${codeship_build_status////\\/} # escape
# or
s%{{CODESHIP_CODE}}%$codeship_build_status%g #
Make sure the new delimiter isn't contained in the string.
Upvotes: 2