我编写了一个脚本将 .CSV 更改为 json。
#!/bin/bash
exec 0< example.csv
while IFS=, read name element input decrease
do
echo "${element}decrease: ${decrease}test"
done
我粘贴在这里的 example.csv
name1,A,11,12
name2,B,13,14
但是输出真的很奇怪......
testrease: 12
testrease: 14
如您所见,test
重写Adecrease
和Bdecrease
,使他们成为 testrease
.
我简直不敢相信!!所以我尝试了 exec 0< example.csv
,在 stdin 中输入它们,这次我得到了我想要的
name1,A,11,12
Adecrease: 12test
所以我想 example.csv 中可能有一些字符我看不出是什么导致了这个问题。我用cat -v example.csv
name1,A,11,12^M
name2,B,13,14^M
没什么奇怪的,我就卡在这里了。
我对 shell 脚本非常陌生,所以如果有人能给我一些建议,我会非常兴奋!!
最佳答案
谢谢你,@chepner ! tag wiki在这个愚蠢的问题上又节省了我一个小时。
这是来自 wiki 的解决方案:
Check whether your script or data has DOS style end-of-line characters.
Use
cat -v yourfile
orecho "$yourvariable" | cat -v
.DOS carriage returns will show up as
^M
after each line.If you find them, delete them using
dos2unix
(a.k.a.fromdos
) ortr -d '\r'
.
关于linux - Bash 回显奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52803080/