通过 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 read 并将分支提取到变量 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
问题是正在获取 "fatal: remote part of refspec is not a valid name in :?[mbugfix/CRIP-2475" 的输出。这里 bugfix/CRIP-2475 是分支名称。我想知道那些额外的字符是什么 :?[m 附加在分支名称之前。
如果我在 git 删除之前执行 echo $mybr,我会得到正确打印为 "bugfix/CRIP-2475" 的值。如果我手动将此值传递给 git delete,它工作正常。但是当它作为变量传递时,出现上述错误。我怀疑前面有一些特殊字符,可能是 ctrlM 字符或 echo 没有打印到屏幕上的东西。
有没有办法删除那些多余的字符?
是的,您可以使用 Bash sub-string replacement
删除
${str/#find/replace}
用于替换 pre-fix 个字符
${str/%find/replace}
用于替换 post-fix 个字符
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
这些"magical"个符号当然是colors。你不应该在管道中使用颜色:
git for-each-ref --format='%(align:1,left)%(authorname)%(end) %(refname:strip=3)' --sort=authorname refs/remotes | …
我需要删除最近离开组织的开发人员 git 存储库中的分支。因此,我使用 git for-each-ref 列出所有分支,然后使用 grep 按开发人员名称过滤结果(以下脚本中的 test_developer)。我使用 shell read 并将分支提取到变量 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
问题是正在获取 "fatal: remote part of refspec is not a valid name in :?[mbugfix/CRIP-2475" 的输出。这里 bugfix/CRIP-2475 是分支名称。我想知道那些额外的字符是什么 :?[m 附加在分支名称之前。
如果我在 git 删除之前执行 echo $mybr,我会得到正确打印为 "bugfix/CRIP-2475" 的值。如果我手动将此值传递给 git delete,它工作正常。但是当它作为变量传递时,出现上述错误。我怀疑前面有一些特殊字符,可能是 ctrlM 字符或 echo 没有打印到屏幕上的东西。
有没有办法删除那些多余的字符?
是的,您可以使用 Bash sub-string replacement
删除${str/#find/replace}
用于替换 pre-fix 个字符
${str/%find/replace}
用于替换 post-fix 个字符
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
这些"magical"个符号当然是colors。你不应该在管道中使用颜色:
git for-each-ref --format='%(align:1,left)%(authorname)%(end) %(refname:strip=3)' --sort=authorname refs/remotes | …