国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Linux > 正文

shell實(shí)現(xiàn)字符編碼轉(zhuǎn)換工具分享

2019-10-26 18:39:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼如下:
#!/bin/bash

: << mark
轉(zhuǎn)碼工具,支持UTF-8轉(zhuǎn)GBK和GBK轉(zhuǎn)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
}


#將當(dāng)前目錄下所有普通文件進(jìn)行轉(zhuǎn)碼 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)

   #進(jìn)行轉(zhuǎn)碼
   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)

   #進(jìn)行轉(zhuǎn)碼
   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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘洛县| 惠州市| 藁城市| 汾西县| 禹城市| 容城县| 应城市| 静安区| 刚察县| 博乐市| 玉田县| 娄底市| 宜宾市| 贵州省| 安康市| 海口市| 旌德县| 昆明市| 怀宁县| 东光县| 美姑县| 勃利县| 英山县| 左贡县| 安图县| 邹平县| 文昌市| 汤阴县| 萝北县| 金湖县| 喀喇| 子长县| 沾益县| 杨浦区| 五指山市| 广灵县| 乌兰察布市| 达州市| 文水县| 泰兴市| 陆河县|