开发者

Need help with my bash script

need some help with my script. I wanna convert ISO files to UTF-8. The problem is I don't know how to write the IF:

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

right and how can tell the sed program - that it ignores # comments ?

Here's my script :

#!/bin/bash

clear

ech开发者_如何学运维o -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

thanks


There appear to be more than a few things wrong in your script (for example, I don't see the "UTF8" variable defined anywhere), but you've made it extremely difficult on yourself in terms of debugging it. If it were me, I would:

  1. put all the find's sh -c "... crap in a separate script so you can test it separately
  2. if [ "`file -b $1`" = ...
    
  3. probably put the sed stuff in a separate function and test that

  4. not use sed -n and then explicitly p; every line, that's silly
  5. properly quote the sed script; I believe you are trying to do redirection inside it

... five suggestions should be enough to get you started. Suggestion 0 is "write a more specific title for your question"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜