Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Concatenating Strings Improperly

Tags:

bash

I have a text file with a list of Mercurial repositories in it, in the form:

IDE
Install
InstallShield

I'm writing a bash script to clone/pull/update all the repositories based on the text file. Right now I'm just echoing before I do any actual cloning. If I do:

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

The output is as expected:

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

However, if I do:

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

The output is:

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

It seems to be replacing the beginning of the string with the end of the string. Is there some kind of character overflow or something going on? My apologies if this is a dumb question, but I'm a relative noob for bash.

like image 989
Chris Bush Avatar asked Dec 05 '25 03:12

Chris Bush


2 Answers

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.

like image 166
chepner Avatar answered Dec 12 '25 05:12

chepner


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

like image 45
CDspace Avatar answered Dec 12 '25 05:12

CDspace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!