3

我有一个文本文件,其中包含 Mercurial 存储库列表,格式为:

IDE
Install
InstallShield

我正在编写一个 bash 脚本来克隆/拉取/更新基于文本文件的所有存储库。现在我只是在进行任何实际克隆之前附和。如果我做:

while read line; do
    echo "hg clone" ${MASTER_HG}/${line};
done < Repos.txt

输出如预期:

hg clone /media/fs02/IDE
hg clone /media/fs02/Install
hg clone /media/fs02/InstallShield

但是,如果我这样做:

while read line; do
    echo "hg clone" ${MASTER_HG}/${line} ${REPOROOT}/${line};
done < Repos.txt

输出是:

/var/hg/repos/IDE02/IDE 
/var/hg/repos/Installnstall
/var/hg/repos/InstallShieldShield

它似乎正在用字符串的结尾替换字符串的开头。是否有某种字符溢出或发生了什么?如果这是一个愚蠢的问题,我深表歉意,但我是 bash 的相对菜鸟。

4

2 回答 2

6

Your file has DOS line endings; the \r at the end of $line causes the cursor to return to the beginning of the line, which only affects your output when $line is not the last thing being printed before the newline. You should remove them with something like dos2unix.


You can use something similar to Perl's chomp command to remove a trailing carriage return, if one is present:

# $'\r' is bash-only, but easy to type. For POSIX shell, you'll need to find
# someway of entering ASCII character 13; perhaps Control-V Control-M
line=${line%$'\r'}

Useful if, for whatever reason, you can't (or don't want to) fix the input before reading it.

于 2013-07-31T20:48:51.483 回答
1

From the looks of it, ${REPOROOT} might already include ${line}, try echoing ${REPOROOT} by itself and see what you get.

于 2013-07-31T20:50:52.017 回答