shell 脚本 转码目录下文件

#!/bin/bash
#递归遍历源文件目录 
function scandir(){  
    if [ $# -lt 3 ]
       then
         echo usage:$0 src sourcecode destcode ;
         exit;
    fi
    local cur_dir workdir sourcecode destcode
    workdir=$1
    sourcecode=$2;
    destcode=$3;
    cd ${workdir}
    if [ ${workdir} = "/" ]
    then
        cur_dir=""
    else
        cur_dir=$(pwd)
    fi

    for dirlist in $(ls ${cur_dir})
    do
        if test -d ${dirlist}
          then
              cd ${dirlist}
              scandir ${cur_dir}/${dirlist} $sourcecode $destcode
              cd ..
        else
            tfile=${cur_dir}/${dirlist}
            echo $tfile
            transFileCode "$sourcecode"  "$destcode"  "$tfile"; 
        fi
    done

}  

#转码文件
function transFileCode(){  
    sourcecode=$1;
    destcode=$2;
    tfile=$3;
    iconv -f $sourcecode -t $destcode $tfile > ${tfile}temp;  
    mv "${tfile}temp" "${tfile}"
}
 
dirname=$1;  #源文件目录
sourcecode=$2;
destcode=$3;
echo '********************************************************************************';  
scandir $dirname $sourcecode $destcode;  #在这里进行递归遍历
echo '********************************************************************************';  

你可能感兴趣的:(shell)