【问题标题】:How to preserve line endings when extracting git commit messages in a bash script?在 bash 脚本中提取 git commit 消息时如何保留行尾?
【发布时间】:2016-03-18 09:31:48
【问题描述】:

我正在使用 git svn 访问 SVN 服务器。 我正在尝试编写一个脚本,将所有 SVN 标签转换并复制到 git 标签中。

我发现了这个: http://gitready.com/advanced/2009/02/16/convert-git-svn-tag-branches-to-real-tags.html

还有这个: https://stackoverflow.com/a/3357357/882697

要创建它:

git for-each-ref refs/remotes/origin/tags | cut -d / -f 5- |
while read ref
do
    msg=`git log --format=%B -n 1 origin/tags/"$ref"`
    echo $msg
done

git log 命令在 mingw bash 中运行良好(来自 git install)。 但是当从同一个 mingw bash 启动脚本时,行尾会被删除。 我试着加了\r

msg=`echo $msg | sed 's/\n/\r\n/'`

但它也不起作用......

有人可以帮帮我吗?谢谢!

【问题讨论】:

    标签: git bash mingw git-svn line-endings


    【解决方案1】:

    尝试至少加上双引号:

    echo "${msg}"
    

    (见“how do I preserve newlines in a quoted string in bash?”)

    这将避免回显解释$msg 内容的输出(执行field splitting or pathname expansion)。

    【讨论】:

    • 非常感谢您的回答和有用的链接。我不仅学到了关于 bash 的技巧,而且现在我的书签中还有 shellcheck.net!再次感谢!
    • @Plouff 不客气!关于拼写检查,这里是关于github.com/koalaman/shellcheck/wiki/SC2086
    • 我没有注意到拼写检查的输出提供了文档的链接。再次感谢这个提示:)!
    猜你喜欢
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多