This general question has been asked many times and almost always there is an obvious syntax problem. But this seems correct:
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
gives
bash: syntax error near unexpected token `done'
Whether pasted as multiline or in one-line mode. With or without the final > utf8_encoded.txt. With the inner quotes escaped or not.
What could be wrong?
whileright after the pipe in the previous line.catto loop over filescatto loop over single files (cat filea fileb | while ...is fine, assuming it is ok to run the loop in a subshell). For single files,while ...; done < mixed_encoded.txtis the preferred style.