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