需要帮助我的bash脚本

时间:2011-04-03 20:50:51

标签: linux bash unix sed iconv

我的脚本需要一些帮助。我想将ISO文件转换为UTF-8。问题是我不知道如何写IF:

if [ `file -b {}` = "$UTF8" ] \ 

对,如何告诉sed程序 - 它忽略了#comments?

这是我的剧本:

#!/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 个答案:

答案 0 :(得分:1)

您的脚本中似乎存在多个错误(例如,我没有在任何地方看到“UTF8”变量),但是在调试它时,您已经非常困难。如果是我,我会:

  1. 将所有发现的sh -c "...垃圾放在一个单独的脚本中,以便您可以单独测试
  2. if [ "`file -b $1`" = ...
    
  3. 可能会将sed内容放在一个单独的函数中并测试

  4. 不使用sed -n,然后明确p;每行,这是愚蠢的
  5. 正确引用sed脚本;我相信你正在尝试在其中进行重定向
  6. ......五个建议应该足以让你入门。建议0是“为您的问题写一个更具体的标题”