濮阳杆衣贸易有限公司

主頁 > 知識庫 > shell實(shí)現(xiàn)字符編碼轉(zhuǎn)換工具分享

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

熱門標(biāo)簽:打電話機(jī)器人接我是他的秘書 地圖標(biāo)注員都是年輕人 揭陽智能電話機(jī)器人推薦 百度地圖標(biāo)注錯了有責(zé)任嗎 如果做線上地圖標(biāo)注 江蘇云電銷機(jī)器人公司 客服外呼系統(tǒng)怎么樣 華鋒e路航港口地圖標(biāo)注 河南信譽(yù)好的不封卡電話外呼系統(tǒng)

復(fù)制代碼 代碼如下:

#!/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

您可能感興趣的文章:
  • 基于字符集、字符編碼與HTTP編碼解碼之萬象詳解
  • Java字符編碼解碼的實(shí)現(xiàn)詳解
  • Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問題解決
  • php字符編碼轉(zhuǎn)換之gb2312轉(zhuǎn)為utf8
  • mysql修改數(shù)據(jù)庫編碼(數(shù)據(jù)庫字符集)和表的字符編碼的方法
  • 微信開發(fā)中mysql字符編碼問題

標(biāo)簽:馬鞍山 赤峰 許昌 金昌 邵陽 淘寶邀評 巴彥淖爾 婁底

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《shell實(shí)現(xiàn)字符編碼轉(zhuǎn)換工具分享》,本文關(guān)鍵詞  shell,實(shí)現(xiàn),字符,編碼,轉(zhuǎn)換,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《shell實(shí)現(xiàn)字符編碼轉(zhuǎn)換工具分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于shell實(shí)現(xiàn)字符編碼轉(zhuǎn)換工具分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    菏泽市| 牙克石市| 永康市| 东阿县| 新干县| 三门县| 伊春市| 阜宁县| 太和县| 克东县| 宜都市| 杭州市| 出国| 类乌齐县| 九龙县| 宁津县| 瑞安市| 信阳市| 玛纳斯县| 建宁县| 丰原市| 芜湖市| 安龙县| 礼泉县| 八宿县| 宿松县| 凤山市| 搜索| 阳曲县| 汤原县| 报价| 阿坝县| 舒城县| 常州市| 深泽县| 潼南县| 道孚县| 那曲县| 广德县| 遵义市| 沾益县|