主頁 > 知識庫 > shell腳本中使用iconv實現批量文件轉碼的代碼分享

shell腳本中使用iconv實現批量文件轉碼的代碼分享

熱門標簽:陜西電銷卡外呼系統怎么安裝 旅游地圖標注大全 excel地址地圖標注 百度地圖的地圖標注 佛山高德地圖標注中心 百度地圖標注圖標更換 東莞電銷機器人價格一覽表 地圖標注超出范圍怎么辦 杭州機器人外呼系統

在開發中,我們經常需要對N多文件編碼進行更改,iconv只能對單文件的進行更改,怎么辦呢?我們寫一個shell腳本來解決這個問題。

例子一:使用shell腳本實現批量轉碼的操作。

#!/bin/sh 
#
convertCodeFilePath=$1 
fromCode=$2 
toCode=$3 
 
for i in {1..1} 
do 
  [ -f $convertCodeFilePath ] 
  if [ $? -eq 0 ] 
  then 
    iconv -f $fromCode -t $toCode -c -o $convertCodeFilePath $convertCodeFilePath 
    if [ $? -ne 0 ] 
    then 
      echo $convertCodeFilePath "=>" convert code failed.      
    else 
      echo $convertCodeFilePath "=>" convert code success. 
    fi 
    break; 
  fi 
   
  [ -d $convertCodeFilePath ] 
  if [ $? -ne 0 ] 
  then 
    break; 
  fi 
     
  dir=`ls $convertCodeFilePath | sort -d` 
 
  for fileName in $dir 
  do 
    fileFullPatch=$convertCodeFilePath/$fileName 
     
    fileType=`echo $fileName |awk -F. '{print $2}'` 
     
    [ -d $fileName ] 
    if [ $? -eq 0 ] 
    then 
      continue 
    fi 
     
    if [ $fileType != 'sh' ]  [ $fileType != 'py' ]  [ $fileType != 'xml' ]  [ $fileType != 'properties' ] \ 
     [ $fileType != 'q' ]  [ $fileType != 'hql' ]  [ $fileType != 'txt' ] 
    then 
      continue 
    fi 
     
    iconv -f $fromCode -t $toCode -c -o $fileFullPatch $fileFullPatch 
    if [ $? -ne 0 ] 
    then 
      echo $fileName "=>" convert code failed. 
      continue 
    else 
      echo $fileName "=>" convert code success. 
    fi     
  done 
done

使用方式:

sh convertCode.sh /home/sam/data gbk utf-8

例子二:簡潔版

用法示例:

cd ~/workspace/XXXProject
 
~/iconv_shell.sh ./ *java

好了,直接上代碼~~

#!/bin/bash
 
if [ "$#" != "2" ]; then
 
  echo "Usage: `basename $0` dir filter"
 
  exit
 
fi
 
dir=$1
 
filter=$2
 
echo $1
 
for file in `find $dir -name "$2"`; do
 
  echo "$file"
 
  iconv -f gbk -t utf8 -o $file $file
 
done

這里還有些修改的余地,比如源和目標編碼應該作為參數,我暫時沒有這個需求,懶得寫了。

您可能感興趣的文章:
  • shell腳本實現統計文件大小、批量創建用戶的示例
  • Shell腳本之文件批量創建與修改的簡單方法
  • Linux shell利用sed如何批量更改文件名詳解
  • Shell腳本實現批量替換文件內容
  • 詳解linux下批量替換文件內容的三種方法(perl,sed,shell)
  • Shell腳本批量修改文件后綴名代碼分享
  • Shell腳本批量重命名文件后綴的3種實現
  • Shell腳本遍歷目錄并批量修改文件編碼
  • shell批量創建文件并重新命名的實例代碼

標簽:延邊 通遼 青島 朝陽 雅安 南充 隨州 西藏

巨人網絡通訊聲明:本文標題《shell腳本中使用iconv實現批量文件轉碼的代碼分享》,本文關鍵詞  shell,腳本,中,使用,iconv,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《shell腳本中使用iconv實現批量文件轉碼的代碼分享》相關的同類信息!
  • 本頁收集關于shell腳本中使用iconv實現批量文件轉碼的代碼分享的相關信息資訊供網民參考!
  • 推薦文章
    2020久久精品亚洲|久久窝窝国产精品午夜看片|亚洲 日韩 激情 无码 中出|国产精品乱码高清在线