linux批量转换整个目录下的文件编码为UTF-8
发布时间:2020-12-15 07:09:03 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/bin/bash - #=============================================================================== # # FILE: conv.sh # # USAGE: ./conv.sh # # DE
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/bin/bash - #=============================================================================== # # FILE: conv.sh # # USAGE: ./conv.sh # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: 目前不支持传入参数中含有空格; # NOTES: --- # AUTHOR: linkscue (scue),[email?protected] # CREATED: 2013年03月06日 22时52分31秒 HKT # COPYRIGHT: Copyright (c) 2013,linkscue # REVISION: 0.1 # ORGANIZATION: --- #=============================================================================== __ScriptVersion="0.1" #=== FUNCTION ================================================================ # NAME: usage # DESCRIPTION: Display usage information. #=============================================================================== function usage () { cat <<- EOT Usage : $0 -s suffix1 -s suffix2 -d dir1 -d dir2 -f file1 -f file2 Options: -h|help Display this message -v|version Display script version -s suffix Setting suffix -d directory Convert all file encoding to UTF-8 -f file Convert a file encoding to UTF-8 EOT } # ---------- end of function usage ---------- #----------------------------------------------------------------------- # Handle command line arguments #----------------------------------------------------------------------- suffixs=() directorys=() files=() while getopts ":hvd:f:s:" opt do case $opt in h|help ) usage; exit 0 ;; v|version ) echo "$0 -- Version $__ScriptVersion"; exit 0 ;; f ) files+=("$OPTARG") ;; d ) directorys+=("$OPTARG") ;; s ) suffixs+=("$OPTARG") ;; ? ) echo -e "n Option does not exist : $OPTARGn" usage; exit 1 ;; esac # --- end of case --- done shift $(($OPTIND-1)) # 检查输入 if [[ ${#files} -lt 1 ]] && [[ ${#directorys} -lt 1 ]]; then usage exit fi TMPFILE="$(mktemp -t convXXXXXX)" trap "rm -f '$TMPFILE'" 0 # EXIT trap "rm -f '$TMPFILE'; exit 1" 2 # INT trap "rm -f '$TMPFILE'; exit 1" 1 15 # HUP TERM #------------------------------------------------------------------------------- # 转换编码函数 #------------------------------------------------------------------------------- conv_utf8(){ file="$1" echo "处理文件: '$file' ..." iconv -f gb2312 -t UTF-8 "$file" -o $TMPFILE 2> /dev/null && mv -f $TMPFILE "$f" || { echo "转换失败: '${file}'" } } # 转换文件 for f in "${files[@]}"; do conv_utf8 "$f" done # 转换目录文件 if [[ ${#directorys} -gt 1 ]]; then if [[ ${#suffixs} -lt 1 ]]; then echo echo "请指定需转换编码的文件后缀,如 '-s txt -s java'" echo usage exit else for s in "${suffixs[@]}"; do for f in $(find $directorys -type f -name "*.${s#.}"); do conv_utf8 "$f" done done fi fi 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- shell脚本进阶一(for,while,continue,break,select等等
- BOOTSTRAP caused an access violation in IMSCUI.DLL at 0
- Webservice设计和模式
- twitter-bootstrap – 正确的方式添加填充到Bootstrap主体,
- 一个简单的BizTalk+WebService+Mapping+Soap Adapter 的Dem
- Observable详解
- 如何在shell中运行python程序而不键入“python”
- Angular2之入门示例
- AngularJS Batarang – 什么是interceptedExpressions?
- scala – 检查我的枚举值中是否存在某些字符串而没有NoSuch