Linux shell 执行报错 字符编码转换 iconv/dos2unix 将 gbk/gb2312 转utf8 解决方案

简介: 字符转换,字符乱码
#!/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="UTF-8"
        # 定义转为 目标编码. 可以互相转换

        fileTypeUtf8=`file $file | grep $destBM`
        fileTypeUtf8Length=${#fileTypeUtf8} 

        echo "ISO-8859  Non-ISO  编码等于 GB2312 编码; 重点: ASCI 在 utf8 和 gbk 中都存在,因此不要将 ASCI 编码进行转换 utf8 " ; 

        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(){
    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
目录
相关文章
|
16天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
43 1
|
1天前
|
存储 监控 中间件
Linux双机热备解决方案之Heartbeat
Linux双机热备解决方案之Heartbeat
|
1天前
|
Ubuntu Linux 开发工具
WSL2(3)安装Linux headers完美解决方案
WSL2(3)安装Linux headers完美解决方案
3 0
|
14天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
17天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
23天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
15 0
|
30天前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
50 0
|
30天前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1