#!/bin/bash
#author pengzhi
#email pengzhi@baidu.com
#brief 批量转换文件编码
source ./confs/conf.sh
function main()
{
file="${1}"
if [ -f "${file}" ];then
iconv_file "${file}"
elif [ -d "${file}" ];then
iconv_dir "${file}"
else
echo "....";
fi
}
function iconv_dir()
{
local dir="${1}";
echo -e "${ESC_START}${COLOR_INFO} Start iconv dir:${dir} ${ESC_END}";
local filelist=`ls "${dir}"`
local item;
for item in ${filelist}
do
if [[ "." == "${item}" || ".." == "${item}" ]];then
continue;
fi
local path="${dir}/${item}";
if [ -d "${path}" ];then
iconv_dir "${path}"
elif [ -f "${path}" ];then
iconv_file "${path}"
else
echo -e "${ESC_START}${COLOR_WARNING} Unknow type:${path} ${ESC_END}";
fi
done
echo -e "${ESC_START}${COLOR_INFO} End iconv dir:${dir} ${ESC_END}";
}
function iconv_file()
{
file="${1}"
echo -e "${ESC_START}${COLOR_INFO} Start iconv file:${file} ${ESC_END}";
t=`date +"%Y%m%d%H%H%s"`
dir=`dirname "${file}"`
tmp="${dir}/.tmp/${t}"
mkdir -p "${dir}/.tmp"
touch "${tmp}"
echo -e "${ESC_START}${COLOR_NOTICE} touch ${tmp} ${ESC_END}";
echo "iconv -c -f UTF-8 -t GB2312 ${file} > ${tmp}"
#(iconv -c -f "UTF-8" -t "GB2312" "${file}" > "${tmp}";exit $?)&
#wait
iconv -c -f "UTF-8" -t "GB2312" "${file}" > "${tmp}"
if [ "$?" -eq 0 ];then
echo "cp -rf ${tmp} ${file}";
cp -rf "${tmp}" "${file}";
else
echo -e "${ESC_START}${COLOR_WARNING} iconv ${file} fail! ${ESC_END}";
fi
rm "${tmp}" -rf
rm -rf "${dir}/.tmp"
echo -e "${ESC_START}${COLOR_INFO} End iconv file:${file} ${ESC_END}";
}
main $1
#author pengzhi
#email pengzhi@baidu.com
#brief 批量转换文件编码
source ./confs/conf.sh
function main()
{
file="${1}"
if [ -f "${file}" ];then
iconv_file "${file}"
elif [ -d "${file}" ];then
iconv_dir "${file}"
else
echo "....";
fi
}
function iconv_dir()
{
local dir="${1}";
echo -e "${ESC_START}${COLOR_INFO} Start iconv dir:${dir} ${ESC_END}";
local filelist=`ls "${dir}"`
local item;
for item in ${filelist}
do
if [[ "." == "${item}" || ".." == "${item}" ]];then
continue;
fi
local path="${dir}/${item}";
if [ -d "${path}" ];then
iconv_dir "${path}"
elif [ -f "${path}" ];then
iconv_file "${path}"
else
echo -e "${ESC_START}${COLOR_WARNING} Unknow type:${path} ${ESC_END}";
fi
done
echo -e "${ESC_START}${COLOR_INFO} End iconv dir:${dir} ${ESC_END}";
}
function iconv_file()
{
file="${1}"
echo -e "${ESC_START}${COLOR_INFO} Start iconv file:${file} ${ESC_END}";
t=`date +"%Y%m%d%H%H%s"`
dir=`dirname "${file}"`
tmp="${dir}/.tmp/${t}"
mkdir -p "${dir}/.tmp"
touch "${tmp}"
echo -e "${ESC_START}${COLOR_NOTICE} touch ${tmp} ${ESC_END}";
echo "iconv -c -f UTF-8 -t GB2312 ${file} > ${tmp}"
#(iconv -c -f "UTF-8" -t "GB2312" "${file}" > "${tmp}";exit $?)&
#wait
iconv -c -f "UTF-8" -t "GB2312" "${file}" > "${tmp}"
if [ "$?" -eq 0 ];then
echo "cp -rf ${tmp} ${file}";
cp -rf "${tmp}" "${file}";
else
echo -e "${ESC_START}${COLOR_WARNING} iconv ${file} fail! ${ESC_END}";
fi
rm "${tmp}" -rf
rm -rf "${dir}/.tmp"
echo -e "${ESC_START}${COLOR_INFO} End iconv file:${file} ${ESC_END}";
}
main $1