shell学习(二)
2018-09-06 本文已影响19人
George_Luofz
需求:将UI输出素材,中文名转化为拼音【英文】
eg: 首页@2x.png --> shouye@2x.png
脚本如下:
#/!bin/sh
CurrentDir=`pwd`
echo $CurrentDir
cd $CurrentDir
suffix='png'
# 先处理掉文件名中的空格
deleteFileNameSpace(){
for x in `ls | tr " " "_"` #空格先替换_
do
f="`echo ${x} | tr "_" " "`"
f1="`echo ${x}| sed 's/_//g'`"
#echo $f1
if [ "$f" != "$f1" ]
then
mv "$f" "$f1"
fi
done
}
deleteFileNameSpace
#开始修改中文
change(){
for file in $(ls $CurrentDir | grep .${suffix})
do
echo $file
#提取@2x之前的内容
local tempFileName=${file%@*}
local tempFileSuffix=${file##*@}
echo "fileName= $tempFileName, suffix= @$tempFileSuffix"
# ch2py修改文件名
local newFileName=$(ch2py "$tempFileName" -s _)
echo "tempName= $newFileName"
# 更改文件
local finishFileName=$newFileName@$tempFileSuffix
mv $file $finishFileName
echo "change $file -> $finishFileName"
done
}
change
echo "change successd!"
备注:
mac需安装ch2py工具
sudo gem install chinese_pinyin
实现的效果:
文件名中文转拼音