實(shí)用腳本分享
最近工作需要嘿悬,寫了幾個(gè)腳本低零,介紹給需要的同學(xué)婆翔,順便說下實(shí)現(xiàn)的原理
地址:ShellSnippet
檢測(cè)iOS主題資源包圖片大小的腳本
使用步驟
該腳本用于檢測(cè)iOS的資源包大小是否符合iOS的資源大小規(guī)范,防止因?yàn)閳D標(biāo)大小的不規(guī)范導(dǎo)致在不同屏幕的機(jī)型上體驗(yàn)不一致掏婶。
使用步驟如下:
1啃奴、先把資源包解壓,資源文件會(huì)放在同一個(gè)目錄下雄妥,演示資源包存放的路徑是:
/Users/aron/GitRepos/iosdemos/YTThemeManagerDemo/Doocuments/IOS
2最蕾、然后執(zhí)行checkImage.sh腳本,如果提示沒有權(quán)限老厌,使用命令chmod u+x ./checkImages.sh
給腳本添加運(yùn)行權(quán)限瘟则,會(huì)提示輸入目錄,復(fù)制粘貼第一步的目錄即可
3枝秤、執(zhí)行了腳本之后會(huì)輸出一個(gè)分析結(jié)果的文件
outLogFile.txt
醋拧,該文件中保存了可能存在問題的資源文件列表比如
nav_btn_share_black
這個(gè)文件2x和3x的大小是一樣的,在3x的機(jī)器上顯示就會(huì)存在問題:圖標(biāo)變小或者圖標(biāo)的顯示效果變模糊淀弹,需要讓UI提供合適的圖標(biāo)丹壕。實(shí)現(xiàn)分析
實(shí)現(xiàn)步驟如下,主要使用了sips
命令獲取圖片的信息垦页,進(jìn)行循環(huán)比較:
-
ls ${imagesFolder}
命令獲取文件夾下的所有資源文件 -
sips -g pixelWidth ${imagePath}
命令獲取圖片資源的信息 - 記錄上個(gè)圖片資源的信息雀费,然后循環(huán)比較判斷圖片尺寸是否合適
對(duì)應(yīng)的代碼如下,關(guān)鍵的地方有做了注釋了:
#/bin/sh
imagesFolder="/Users/aron/Desktop/CheckImages/IOS"
outpuLogFile="$(pwd)/outLogFile.txt"
echo "" > ${outpuLogFile}
# 輸入文件夾
echo -n "請(qǐng)輸入圖片目錄: "
read imagesFolder
if [[ -d $imagesFolder ]]; then
echo "圖片目錄正確"
else
echo -n "輸入的目錄無效痊焊,請(qǐng)重新執(zhí)行腳本"
fi
# 獲取下面的所有文件
lastHandleFile=""
lastHandleImageWidthHeight=""
for imageFullName in $(ls ${imagesFolder}); do
echo "imageFullName = ${imageFullName} lastHandleFile = ${lastHandleFile}"
// 使用sed的替換命令s盏袄,去掉圖片名稱中的 @2x.png @3x.png 結(jié)尾,方便比較
pureImageName=`echo ${imageFullName} | sed 's/@.*.png//g' | sed 's/\.png//g'`
if [[ ${pureImageName} == ${lastHandleFile} ]]; then
# 判斷大小
imagePath="${imagesFolder}/${imageFullName}"
// sips -g pixelWidth ${imagePath} 獲取到的結(jié)果如下:
// pixelHeight: 1334
// 使用awk命令使用:分隔符分割獲取第二部分也就是圖片的寬度
ImageWidth=`sips -g pixelWidth ${imagePath} | awk -F: '{print $2}'`
ImageHeight=`sips -g pixelHeight ${imagePath} | awk -F: '{print $2}'`
height=`echo $ImageHeight`
width=`echo $ImageWidth`
currentImageWidthHeight="${width}_${height}"
if [[ ${currentImageWidthHeight} == ${lastHandleImageWidthHeight} ]]; then
// 使用>>追加寫入可能不符合要求的文件名
echo ${lastHandleFile} >> ${outpuLogFile}
fi
else
lastHandleFile=${pureImageName}
imagePath="${imagesFolder}/${imageFullName}"
ImageWidth=`sips -g pixelWidth ${imagePath} | awk -F: '{print $2}'`
ImageHeight=`sips -g pixelHeight ${imagePath} | awk -F: '{print $2}'`
height=`echo $ImageHeight`
width=`echo $ImageWidth`
lastHandleImageWidthHeight="${width}_${height}"
echo "pureImageName = ${pureImageName} lastHandleImageWidthHeight=${lastHandleImageWidthHeight}"
fi
done
批量文件轉(zhuǎn)換編碼的腳本
把源文件的編碼轉(zhuǎn)換為UTF-8薄啥,默認(rèn)轉(zhuǎn)換的源代碼是php和html結(jié)尾的源碼辕羽,需要轉(zhuǎn)換其它格式的自定義即可,修改代碼中的如下行
if [[ $(expr "$item" : '.*\.php') -gt 0 ]] || [[ $(expr "$item" : '.*\.html') -gt 0 ]]; then
比如需要轉(zhuǎn)換xxx.m xxx.h xxx.mm xxx.cpp xxx.hpp文件垄惧,這一行的寫法如下
if [[ $(expr "$item" : '.*\.') -gt 0 ]] || [[ $(expr "$item" : '.*\.h') -gt 0 ]] || [[ $(expr "$item" : '.*\.cpp') -gt 0 ]] || [[ $(expr "$item" : '.*\.hpp') -gt 0 ]]; then
使用enca
命令轉(zhuǎn)換刁愿,如果沒安裝,腳本會(huì)自動(dòng)使用brew
安裝enca
命令到逊,如果沒安裝brew
铣口,emmm滤钱。。脑题。請(qǐng)自行百度安裝吧件缸。
使用步驟
1、執(zhí)行命令叔遂,提示輸入源代碼的目錄他炊,腳本會(huì)遍歷該目錄下的源文件,進(jìn)行編碼轉(zhuǎn)換已艰,結(jié)束痊末。
實(shí)現(xiàn)分下
該腳本實(shí)現(xiàn)很簡(jiǎn)單,主要是read_implement_file_recursively
方法中處理遞歸遍歷哩掺,判斷文件的擴(kuò)展名是否是php/html文件凿叠,符合要求的話使用enca
命令轉(zhuǎn)換格式即可,使用的一些輔助方法包括
- 檢測(cè)命令是否安裝的方法
test_command_installed
疮丛,如果未安裝幔嫂,調(diào)用brew install enca
進(jìn)行安裝enca
- 檢測(cè)文件加是否存在并提示輸入的方法
checkDirCore
和checkInputDestDirRecursive
#!/bin/sh
# 檢測(cè)某個(gè)命令是否安裝
# @return
# 0 未安裝
# 1 安裝
function test_command_installed {
echo "test_command_installed=="
which_command=`which $1`
echo $which_command
if [[ -n $which_command ]]; then
echo "$1 command installed"
return 1
else
echo "$1 command not installed"
return 0
fi
}
# 循環(huán)檢測(cè)輸入的文件夾
checkInputDestDirRecursive() {
echo -n "請(qǐng)輸入目錄: "
read path
if [[ -d $path ]]; then
CheckInputDestDirRecursiveReturnValue=$path
else
echo -n "輸入的目錄無效,"
checkInputDestDirRecursive
fi
}
# 檢測(cè)文件夾存在的方法誊薄,結(jié)果保存在全局變量`CheckInputDestDirRecursiveReturnValue`中
# 參數(shù)一:檢測(cè)的文件夾路徑
# 參數(shù)二:提示消息字符串
# 使用方式如下履恩,去掉注釋
# # 導(dǎo)入工具腳本
# . ./FileUtil.sh
# # 檢測(cè)class_search_dir
# checkDirCore $class_search_dir "指定類的查找目錄不存在"
# class_search_dir=${CheckInputDestDirRecursiveReturnValue}
class_search_dir=""
checkDirCore() {
to_process_dir=$1
message=$2
# 需處理源碼目錄檢查
if [[ -d $to_process_dir ]]; then
echo "目錄存在 $to_process_dir"
CheckInputDestDirRecursiveReturnValue=$to_process_dir
return 1
else
echo "${message} ${to_process_dir}"
checkInputDestDirRecursive ${to_process_dir}
fi
}
# 遞歸函數(shù)讀取目錄下的所有.m文件
function read_implement_file_recursively {
echo "read_implement_file_recursively"
if [[ -d $1 ]]; then
for item in $(ls $1); do
itemPath="$1/${item}"
if [[ -d $itemPath ]]; then
# 目錄
echo "處理目錄 ${itemPath}"
read_implement_file_recursively $itemPath
echo "處理目錄結(jié)束====="
else
# 文件
echo "處理文件 ${itemPath}"
// 判斷是否是.php/.html擴(kuò)展名的文件
if [[ $(expr "$item" : '.*\.php') -gt 0 ]] || [[ $(expr "$item" : '.*\.html') -gt 0 ]]; then
echo ">>>>>>>>>>>>mmmmmmm"
# 使用enca轉(zhuǎn)換格式
enca -L zh_CN -x UTF-8 ${itemPath}
fi
echo ""
fi
done
else
echo "err:不是一個(gè)目錄"
fi
}
test_command_installed enca
result=$?
if [[ $result = 0 ]]; then
echo "enca not installed now install"
brew install enca || exit 1
fi
checkDirCore $class_search_dir "指定目錄不存在"
class_search_dir=${CheckInputDestDirRecursiveReturnValue}
read_implement_file_recursively ${class_search_dir}