问题描述
我有一个README.md文件,其中我想带线$来代替文字标识 {{codeSHIP_ code}}
C $ C - 专门构建状态图像code片段,包括git的分支名
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-解析--abbrev-REF HEAD
在bash变量 - 将模式/字符串变量来搜索。这种模式/字符串只是用来作为标识快速找到其中的README.md文件我想我的输出构建状态图像code。
- 使用
SED
(可能是的grep
)来搜索指定的 {{codeSHIP_ code}} 模式/串并构建图像状态code替换
- Place the current branch
git rev-parse --abbrev-ref HEAD
in bash variable - Place the pattern/string to search for in a variable. This pattern/string is just used as an identifier to quickly locate where in the README.md file I want to output my build status image code.
- Use
sed
(possiblygrep
) to search the README.md file for the specified{{CODESHIP_CODE}}
pattern/string and replace it with the build image status code
在code我写的是这样的:
The 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}} / [...!:坏标志替换命令:'A'
我不知道如何解决这个问题,使脚本正常工作。任何帮助将大大AP preciated。
I'm not sure how to resolve this issue, so that the script works correctly. Any help would be greatly appreciated.
推荐答案
您不能使用包含转义符替换字符串。逃离字符串中的分隔符,或者切换到不同的。
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.
这篇关于对于模式猛砸搜索文件,替换图案code,包括Git的分行名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!