String 将从文件中读取的字符串与字符串文字连接在一起会创建混乱的输出

String 将从文件中读取的字符串与字符串文字连接在一起会创建混乱的输出,string,macos,bash,concatenation,String,Macos,Bash,Concatenation,我的问题是结果很混乱。考虑这个脚本: #!/bin/bash INPUT="filelist.txt" i=0; while read label do i=$[$i+1] echo "HELLO${label}WORLD" done <<< $'1\n2\n3\n4' i=0; while read label do i=$[$i+1] echo "HELLO${label}WORLD" done < "$INPUT" 第一个循环,

我的问题是结果很混乱。考虑这个脚本:

#!/bin/bash
INPUT="filelist.txt"

i=0;
while read label
do
    i=$[$i+1]
    echo "HELLO${label}WORLD"
done <<< $'1\n2\n3\n4'

i=0;
while read label
do
    i=$[$i+1]
    echo "HELLO${label}WORLD"
done < "$INPUT"

第一个循环,带有即时输入(通过我认为称为herestring的东西)(
好吧,就在我即将点击post时,解决方案击中了我。在这里共享以便其他人可以找到它-我花了3个小时调试它(尽管几乎所有时间都是如此),所以我看到了解决这个特定(常见)问题的价值用例

问题是
filelist.txt
是在Windows中创建的。这意味着它有CRLF行结尾,而OSX(与其他类似Unix的环境一样)只需要LF行结尾。(请参阅此处的更多信息:)

在使用之前,我使用转换文件。使用
sed
我只替换了最后一行的回车符,因此我坚持使用已知的方法,并采用
perl
方法。最终脚本如下:

 #!/bin/bash
INPUTFILE="filelist.txt"
INPUT=$(perl -pe 's/\r\n|\n|\r/\n/g' "$INPUTFILE")

i=0;
while read label
do
    i=$[$i+1]
    echo "HELLO${label}WORLD"
done <<< $'INPUT'
!/bin/bash
INPUTFILE=“filelist.txt”
INPUT=$(perl-pe的/\r\n |\n |\r/\n/g'$INPUTFILE)
i=0;
阅读标签时
做
i=$[$i+1]
echo“HELLO${label}WORLD”
完成
HELLO1WORLD
HELLO2WORLD
HELLO3WORLD
HELLO4WORLD
WORLD5
WORLD8
WORLD15
WORLD67
 #!/bin/bash
INPUTFILE="filelist.txt"
INPUT=$(perl -pe 's/\r\n|\n|\r/\n/g' "$INPUTFILE")

i=0;
while read label
do
    i=$[$i+1]
    echo "HELLO${label}WORLD"
done <<< $'INPUT'