#!/bin/bash # gb2312 转 utf8 编码 # 开发 : 小绿叶技术博客 eisc.cn # date : 2023.2.27 dir="./" ; echo "当前目录: $dir" sudo chmod 777 -R $dir/* # FileList=(`du -ah $dir | awk -F" " '{print $2}'`) # find ./ -type f | xargs dos2unix # 列出所有文件和目录 # xargs 作用是将管道前面的结果内容(查找所有内容为 f 文件 ) 传递 给后面命令使用 apt_install() { linuxKernel=`cat /etc/os-release | grep -w ID | awk -F"=" '{print $2}'` case "$linuxKernel" in "ubuntu") sudo apt install -y dos2unix ; echo "[ok] 当前系统为: $linuxKernel 是 ubuntu..." ;; "debian") sudo apt install -y dos2unix ; echo "[ok] 当前系统为: $linuxKernel 是 debian..." ;; "centos") sudo yum install -y dos2unix ; echo "[ok] 当前系统为: $linuxKernel 是 centos..." ;; "redhat") sudo yum install -y dos2unix ; echo "[ok] 当前系统为: $linuxKernel 是 redhat..." ;; *) echo "未匹配到系统,你可以更换系统后再试试, 建议系统: ubuntu20 debian centos redhat" ;; esac sleep 1 } run_file() { destBM="GB2312" # 定义转为 目标编码. 可以互相转换 fileTypeUtf8=`file $file | grep $destBM` fileTypeUtf8Length=${#fileTypeUtf8} case "$bm" in "ISO-8859") bm="GB2312" ;; "Non-ISO") bm="GB2312" ;; esac if [ "$fileTypeUtf8Length" -lt "1" ] then echo "[runing] 编码 $bm 转为 $destBM $file 文件类类型: $fileType" # sudo dos2unix $file sudo iconv -c -f $bm -t $destBM $file -o $file.zhuanhuan sudo mv $file.zhuanhuan $file else echo "[ok] 该文件:$file 已经是 $destBM 编码 该文件编码属性字符串长度为: $fileUtf8Length 文件类类型: $fileType" fi } Character_encoding() { for((i=0;i<${#FileList[*]};i++)) do file=${FileList[$i]} fileType=`file $file ` bmFileBZ=0 bmlist=( GB2312 ISO-8859 Non-ISO GBK UTF-8) # GB2312 ISO-8859 GBK 三种编码是同类,填写 for bm in ${bmlist[*]} do bmFile=`file $file | grep $bm` ; bmFileLength=${#bmFile} ; if [ "$bmFileLength" -gt 0 ] then echo "[runing] 当前文件 $file 编码是 $bm 开始处理 " bmFileBZ=1 run_file fi done # if [ "$x"="0" ] && [ "$y" = "0" ] 多个条件判断 if [ "$bmFileBZ" = "0" ] then echo "[warning] 文件编码标记(0/1 未匹配/匹配编码): $bmFileBZ 当前文件 $file 编码非 ( ${bmlist[*]} ) 编码: `file $file`" fi done } list_file() { szFile=( c h ) for szf in ${szFile[*]} do case "$szf" in "c") echo "当前正在处理 .c 文件" ; FileList=(`find $dir -name "*.$szf"`) ; Character_encoding ;; "h") echo "当前正在处理 .h 文件" ; FileList=(`find $dir -name "*.$szf"`) ; Character_encoding ;; esac done } main(){ echo "ISO-8859 Non-ISO 编码等于 GB2312 编码; 重点: ASCI 在 utf8 和 gbk 中都存在,因此不要将 ASCI 编码进行转换 utf8 " ; apt_install list_file } main # rm -rf ss11-a1; unzip ss11-a1.zip ; cp ~/cs/sh/utf8.sh ss11-a1/ ; chmod +x ss11-a1/utf8.sh ; ss11-a1/utf8.sh ; cat -n ss11-a1/src/APP/GetOutValue/GetOutValue.c | grep 1201 # iconv 编码转换脚本一键执行: wget eisc.cn/file/ubuntu/shell/tools/file/utf8.sh ; sudo chmod +x utf8.sh ; ./utf8.sh