【发布时间】:2016-06-15 00:05:33
【问题描述】:
我有一个 README.md 文件,我想用一行代码替换文本标识符 {{CODESHIP_CODE}} — 特别是包含 git 分支名称的构建状态图像代码 sn-p。
我想它会看起来像这样......
- 将当前分支
git rev-parse --abbrev-ref HEAD放入 bash 变量中 - 将要搜索的模式/字符串放在变量中。此模式/字符串仅用作标识符,用于快速定位 README.md 文件中我想要输出构建状态图像代码的位置。
- 使用
sed(可能是grep)在README.md 文件中搜索指定的{{CODESHIP_CODE}}模式/字符串,并将其替换为构建映像状态代码
我写的代码是这样的:
#!/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="[ ](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
问题是我不断收到以下错误:
sed: 1: "s/{{CODESHIP_CODE}}/[ ! ...": bad flag in substitute command: 'a'
我不确定如何解决此问题,以便脚本正常工作。任何帮助将不胜感激。
【问题讨论】: