unterminated
s'使用El Capitan 10.11.3在我的MacBook上运行sed命令的命令`(不是操作系统版本与此问题有关)
我将diff-so-fancy
与diff-highlight
(python)一起使用。但我发现该问题位于format_diff_header()
bash脚本中的diff-so-fancy
函数中。我将在下面解释。当然,这个帖子也可能有其他原因。
当我运行命令时:git diff
(并且当然在.gitconfig
中配置diff命令以使用pager = diff-highlight | diff-so-fancy | less -r
)
所以命令和错误:
$ git diff
sed: -e expression #1, char 259: unterminated \`s' command
sed: -e expression #1, char 273: unterminated \`s' command
以下git-so-fancy
的完整脚本:
#!/bin/bash
###############
# diff-so-fancy builds on the good-lookin' output of diff-highlight to upgrade your diffs' appearances
# * Output will not be in standard patch format, but will be readable
# * No pesky `+` or `-` at line-stars, making for easier copy-paste.
#
# Screenshot: https://github.com/paulirish/dotfiles/commit/6743b907ff58#commitcomment-13349456
#
#
# Usage
#
# git diff | diff-highlight | diff-so-fancy
#
# Add to .gitconfig so all `git diff` uses it.
# git config --global core.pager "diff-highlight | diff-so-fancy | less -r"
#
#
# Requirements / Install
#
# * GNU sed. On Mac, install it with Homebrew:
# brew install gnu-sed --default-names # You'll have to change below to `gsed` otherwise
# * diff-highlight. It's shipped with Git, but probably not in your $PATH
# ln -sf "$(brew --prefix)/share/git-core/contrib/diff-highlight/diff-highlight" ~/bin/diff-highlight
# * Add some coloring to your .gitconfig:
# git config --global color.diff-highlight.oldNormal "red bold"
# git config --global color.diff-highlight.oldHighlight "red bold 52"
# git config --global color.diff-highlight.newNormal "green bold"
# git config --global color.diff-highlight.newHighlight "green bold 22"
#
###############
# TODO:
# Put on NPM.
[ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
color_code_regex=$'(\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)[m|K])?'
reset_color="\x1B\[m"
dim_magenta="\x1B\[38;05;146m"
format_diff_header () {
# simplify the unified patch diff header
sed -E "s/^($color_code_regex)diff --git .*$//g" | \
sed -E "s/^($color_code_regex)index .*$/\
\1$(print_horizontal_rule)/g" | \
sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(print_horizontal_rule)/g"
}
colorize_context_line () {
# extra color for @@ context line
sed -E "s/@@$reset_color $reset_color(.*$)/@@ $dim_magenta\1/g"
}
strip_leading_symbols () {
# strip the + and -
sed -E "s/^($color_code_regex)[\+\-]/\1 /g"
}
print_horizontal_rule () {
printf "%$(tput cols)s\n"|tr " " "─"
}
# run it.
cat $input | format_diff_header | colorize_context_line | strip_leading_symbols
当我从上面脚本的最后一行中删除| format_diff_header
时,错误消失了,但当然没有解决。
因此引发的错误:sed: -e expression #1: unterminated
s'命令`必须位于函数中的某个位置,再次在下面发布。
format_diff_header () {
# simplify the unified patch diff header
sed -E "s/^($color_code_regex)diff --git .*$//g" | \
sed -E "s/^($color_code_regex)index .*$/\
\1$(print_horizontal_rule)/g" | \
sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(print_horizontal_rule)/g"
}
欢迎所有建议!提前谢谢。
可以找到diff-so-fancy
的原始脚本https://github.com/paulirish/dotfiles/blob/master/bin/diff-so-fancy。