读书人

用ShellScript批量资料的

发布时间: 2013-01-28 11:49:56 作者: rapoo

用ShellScript批量文件的

最近研究了下ShellScript,了一工具,批量文件的UTF-8,理上支持Linux和MacOSX,出源:

明:Script有除源文件的操作,在使用前先份,因此而造成失的本人概不!


#!/bin/bashfunction usage() {echo "";echo "Usage: tencoder <dest_dir> <from_encode>";echo "";exit 1;}function test_encode() {echo "Test encoded file." >> ~/test_encoded.tmp;iconv -f $2 -t UTF8 ~/test_encoded.tmp > ~/test_encoded.tmp || exec sh -c "rm -f ~/test_encoded.tmp; exit 1;";rm -f ~/test_encoded.tmp;}function delete_old_encoded_files() {for file in `ls $1`doif test -d $1"/"$file; thendelete_old_encoded_files $1"/"$file;fidonefor file in `ls $1 | grep encoded_utf8`dorm -f $1"/"$file;done}function doencode() {#utf-8for file in `ls $1`dolocal filepath=$1"/"$file#只文件,文件略if [ ! -d $filepath ]; then#如果成功,除原文件,把後的文件重命名原文件的名字if (iconv -f $2 -t UTF8 $filepath > $filepath.encoded_utf8) ; thenrm -f $filepath;mv $filepath.encoded_utf8 $filepath;#否,失(可能是片文件),除生的文件,保留的原文件else rm -f $filepath.encoded_utf8;fifidone} #遍所有文件function iterate() {for file in `ls $1`doif test -d $1"/"$file; theniterate $1"/"$file $2;fidonedoencode $1 $2;}#如果不等於2,示用法if [ $# -ne 2 ]; thenusage#如果1不是一目的路,提示elif [ ! -d $1 ]; thenecho "";echo "No such diretory -> $1"echo "";exit 1;else#2(是否正)test_encode $1 $2;#除以前的encode的文件delete_old_encoded_files $1;#始遍iterate $1 $2;fi


使用方法:

段script制保存文件,如utf8encoder.sh,加上行限,使用格式如下:

# utf8encoder.sh <需要的文件> <源格式>


例:

# utf8encoder.sh /Users/tim/myfolder GB18030

读书人网 >编程

热点推荐