bash - 遍历标签文件似乎添加了一个问号

标签 bash git

我正在使用 bash 并尝试遍历文件的行,以添加 git 标签:

$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt

鉴于 NEW_TAGS.txt有这个内容:

Test_V1_4.3
Test_V2_2.7

每行以<CR><LF>结尾,上面一行的结果是这样的:

Test_V1_4.3
fatal: 'Test_V1_4.3?' is not a valid tag name.
Test_V2_2.7
fatal: 'Test_V2_2.7?' is not a valid tag name.

问号从何而来?如何删除它们?

或者,是否有更好的方法从文件中向 git 添加标签列表?

最佳答案

问题是换行符。当 dos (Windows) 系统使用 \r\n 时,Linux 和 Unixes 使用 \n 。使用 dos2unix 更改换行符。

[Alex@Normandy tmp]$ cat NEW_TAGS.txt 
Test_V1_4.3
Test_V2_2.7
[Alex@Normandy tmp]$ file NEW_TAGS.txt 
NEW_TAGS.txt: ASCII text
[Alex@Normandy tmp]$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt
Test_V1_4.3
fatal: tag 'Test_V1_4.3' already exists
Test_V2_2.7
fatal: tag 'Test_V2_2.7' already exists
[Alex@Normandy tmp]$ unix2dos NEW_TAGS.txt 
unix2dos: converting file NEW_TAGS.txt to DOS format ...
[Alex@Normandy tmp]$ file NEW_TAGS.txt 
NEW_TAGS.txt: ASCII text, with CRLF line terminators
[Alex@Normandy tmp]$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt
Test_V1_4.3
fatal: 'Test_V1_4.3?' is not a valid tag name.
Test_V2_2.7
fatal: 'Test_V2_2.7?' is not a valid tag name.

关于bash - 遍历标签文件似乎添加了一个问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62041441/

相关文章:

linux - 最常用IP地址bash脚本的Apache访问日志

php - 如何将多个 PHP 参数传递给 bash

linux - 如何将小写字母排序为大写字母

git - Git 上的 Pharo 项目

git : How to delete a remote branch whose name starts with a hashtag '#' ?

git - 使 git pull (rebase) 默认只从当前下游分支 pull

git - 如何使用过滤器分支更改多个提交的提交作者?

linux shell脚本sed css结构

git - 如何仅在 merge 请求 merge 后触发 bash 脚本?

bash - 如何在脚本继续执行其他命令时使 osascript 显示对话框