我正在使用 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/