繁体   English   中英

通过shell脚本读取的git命令的输出在再次传递给另一个git命令时会添加特殊字符

[英]Output of a git command read through shell script is adding special characters when passed again to another git command

我需要删除最近离开组织的开发人员的git存储库中的分支。 因此,我使用git for-each-ref列出所有分支,然后使用grep按开发人员名称(以下脚本中的test_developer)过滤结果。 我使用shell读取并将分支提取到变量mybr中,并使用git push origin --delete删除分支。 请在下面找到代码片段:

git for-each-ref --format='%(align:1,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=authorname refs/remotes | grep test_developer | while read line;do mystr=(${line}); mybr=${mystr[1]}; git push origin --delete "$mybr"; done

问题是输出结果为“致命:refspec的远程部分不是:?[mbugfix / CRIP-2475”中的有效名称。 这里的bugfix / CRIP-2475是分支名称。 我不知道这些额外的字符是什么:?[m在分支名称之前附加。

如果我在git delete之前回显了$ mybr,我会得到正确打印为“ bugfix / CRIP-2475”的值。 而且,如果我将此值手动传递给git delete,它就可以正常工作。 但是,当将其作为变量传递时,出现上述错误。 我怀疑前面有一些特殊字符,可能是ctrlM字符或回声未打印到屏幕上的某些字符。

反正有删除那些多余的字符吗?

是的,您可以使用Bash子字符串替换来删除

${str/#find/replace}用于替换前缀字符

${str/%find/replace}用于替换后缀字符

git for-each-ref --format='%(align:1,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=authorname refs/remotes | grep test_developer | while read line;do mystr=(${line}); mybr=${mystr[1]}; git push origin --delete "${mybr/#?[m/}"; done

这些“魔术”符号当然是颜色 您应该在管道中使用颜色:

git for-each-ref --format='%(align:1,left)%(authorname)%(end) %(refname:strip=3)' --sort=authorname refs/remotes | …

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM