2011-04-03 197 views
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 

感謝

+1

「sed」調用的目標是什麼? I.E.你爲什麼要翻譯這些角色?順便說一句,如果你確實需要這樣做,'tr'更適合這項任務。 – intuited 2011-04-03 21:16:16

+0

其實,沒關係,我只是意識到'tr'不能正確處理Unicode。 – intuited 2011-04-03 21:19:33

+0

我想要ä,ö,ü作爲unicode。謝謝我會檢查tr – user690219 2011-04-03 21:20:12

回答

1

似乎有比幾件事錯在你的腳本更(例如,我沒有看到任何地方所定義的「UTF8」變量) ,但是在調試它的時候,你已經讓自己變得非常困難。如果是我,我會:

  1. 把所有發現的sh -c "...廢話在一個單獨的腳本,以便您可以單獨測試
  2. if [ "`file -b $1`" = ... 
    
  3. 可能把sed的東西,在一個單獨的功能和測試

  4. 不使用sed -n,然後明確每p;線,這是愚蠢的
  5. 正確現狀te se腳本;我相信你正在嘗試做內部重定向

......五個建議應該足以讓你開始。建議0是「爲你的問題寫一個更具體的標題」