您现在的位置是:网站首页> 编程资料编程资料
shell实现字符编码转换工具分享_linux shell_
2023-05-26
359人已围观
简介 shell实现字符编码转换工具分享_linux shell_
#!/bin/bash
: << mark
转码工具,支持UTF-8转GBK和GBK转UTF-8
孔令飞@2012-05-07
mark
#set -x
scode="gbk"
dcode="utf-8"
function Usage()
{
cat << EOF
Usage: conv [OPTIONS] [DIR]
[-u] GBK to UTF-8
[-g] UTF-8 to GBK
EOF
exit 1
}
#将当前目录下所有普通文件进行转码 GBK to UTF-8
function g2u()
{
local dir=$1
printf "Convering $dir ......"
for file in $(ls $dir)
do
file="$dir/$file"
if [ -f $file ];then
coding=$(file -b $file | cut -d ' ' -f1)
#进行转码
if [ "$coding" = "ISO-8859" ];then
local tmpfile=$(mktemp)
Fright=$(stat -c %a $file)
Fuser=$(stat -c %U $file)
Fgro=$(stat -c %G $file)
iconv -f $scode -t $dcode $file > $tmpfile || Usage
mv $tmpfile $file &&
chmod $Fright $file
chown $Fuser:$Fgrp $file
fi
fi
done
printf " done\n"
}
function u2g()
{
local dir=$1
printf "Convering $dir ......"
for file in $(ls $dir)
do
file="$dir/$file"
if [ -f $file ];then
coding=$(file -b $file |cut -d ' ' -f1)
#进行转码
if [ "$coding" = "UTF-8" ];then
local tmpfile=$(mktemp)
Fright=$(stat -c %a $file)
Fuser=$(stat -c %U $file)
Fgro=$(stat -c %G $file)
iconv -f $dcode -t $scode $file > $tmpfile || Usage
mv $tmpfile $file &&
chmod $Fright $file
chown $Fuser:$Fgrp $file
fi
fi
done
printf " done\n"
}
[ $# -ne 2 ] && Usage
while getopts ug opt
do
case $opt in
u) echo "Convert gbk coding to utf-8 ...."
for dir in $(find $2 -type d)
do
g2u $dir
done
g) echo "Convert utf-8 coding to gbk ...."
for dir in $(find $2 -type d)
do
u2g $dir
done
*) Usage
exit 1
esac
done
exit 0
相关内容
- shell编程之实现windows回收站功能分享_linux shell_
- linux数据库备份并通过ftp上传脚本分享_linux shell_
- shell学习教程获取命令行参数示例_linux shell_
- linux删除无效链接文件脚本分享_linux shell_
- nginx日志切割脚本分享_linux shell_
- Shell编程之Shell变量学习总结_linux shell_
- 使用curl递归下载软件脚本分享_linux shell_
- mysql源码安装脚本分享_linux shell_
- 从apnic提取ip信息脚本分享_linux shell_
- 实现批量linux格式化硬盘和挂载硬盘脚本分享_linux shell_
