【发布时间】:2014-12-25 02:17:00
【问题描述】:
这个一般性问题已被问过很多次,而且几乎总是存在明显的语法问题。但这似乎是正确的:
cat mixed_encoded.txt |
while read i do
type=${"$(echo "$i" | file -bi -)"#*=}
if [[ $type == 'iso-8859-1' ]]; then
echo "$i" | iconv -f ISO-8859-1 -t UTF-8
else
echo "$i"
fi
done > utf8_encoded.txt
给予
bash: syntax error near unexpected token `done'
是否以多行或单行模式粘贴。有或没有最后的> utf8_encoded.txt。内部引号是否转义。
可能出了什么问题?
【问题讨论】:
-
尝试将
while放在上一行的管道之后。 -
不要使用
cat循环文件 -
不要使用
cat循环single 文件(cat filea fileb | while ...很好,假设可以在子shell 中运行循环)。对于单个文件,while ...; done < mixed_encoded.txt是首选样式。 -
谢谢@chepner,知道有用。