1

我的脚本需要一些帮助。我想将 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

谢谢

4

1 回答 1

1

您的脚本中似乎有很多错误(例如,我没有看到任何地方定义的“UTF8”变量),但您自己在调试它方面非常困难。如果是我,我会:

  1. 将所有发现的sh -c "...废话放在一个单独的脚本中,以便您可以单独测试它
  2. if [ "`file -b $1`" = ...
    
  3. 可能把这些sed东西放在一个单独的函数中并测试

  4. 不使用sed -n然后明确地p;每一行,这很愚蠢
  5. 正确引用 sed 脚本;我相信您正在尝试在其中进行重定向

...五个建议应该足以让您入门。建议 0 是“为您的问题写一个更具体的标题”

于 2011-04-03T21:37:04.020 回答