1
需要一些帮助,我的脚本。我想将ISO文件转换为UTF-8。问题是我不知道如何写IF:需要我的bash脚本帮助
if [ `file -b {}` = "$UTF8" ] \
正确和如何告诉sed程序 - 它忽略#注释?
这里是我的脚本:
#!/bin/bash
clear
echo -e '\E[37mThis script encodes recursively files from \E[31mISO-8859-1 \E[37mto \E[31mUTF-8 \E[37musing iconv.'
echo "Files of the following coded character sets will be encode: "
echo -e '\E[32m'
a='*.java'
b='*.txt'
c='*.php'
d='*.html'
e='*.aj'
f='*.patch'
g='*.css'
h='*.js'
i='*.conf'
j='*.jsp'
k='*.sh'
l='*.py'
m='*.pl'
n='*.rb'
for x in "$a" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$i" "$j" "$k" "$l" "$m" "$n"
do
echo $x
done
echo
tput sgr0
#
# TODO: COMMENTS aren't ignored
# TOOD: IF-THEN aren't working right
#
for y in "$a" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$i" "$j" "$k" "$l" "$m" "$n"
do
echo -e "\E[37mencoding all <\E[32m$y\E[37m> files ..."
find . -name "$y" -exec sh -c "(\
UTF=".*UTF-8 Unicode.*" \
FILE={} \
if [ `file -b {}` = "$UTF8" ] \
then \
iconv -f latin1 -t UTF-8 {} -o {}.iconv ; \
sed -n '
{
s/^ *#/#/#.*//g;
s/ä/0xE4;/g;
s/Ä/0xC4;/g;
s/ü/0xFC;/g;
s/Ü/0xDC;/g;
s/ö/0xF6;/g;
s/Ö/0xD6;/g;
s/ß/0xDF;/g;
p;
} {}.iconv > {}.iconv_sed \ '
mv {}.iconv_sed {} && rm {}.iconv ; \
else \
echo "$FILE is a UTF8 file. " \
fi \
)" \;
echo -e '\E[33m*** done ***'
done
echo
tput sgr0
exit 0
感谢
“sed”调用的目标是什么? I.E.你为什么要翻译这些角色?顺便说一句,如果你确实需要这样做,'tr'更适合这项任务。 – intuited 2011-04-03 21:16:16
其实,没关系,我只是意识到'tr'不能正确处理Unicode。 – intuited 2011-04-03 21:19:33
我想要ä,ö,ü作为unicode。谢谢我会检查tr – user690219 2011-04-03 21:20:12