實用腳本分享
最近工作需要,寫了幾個腳本,介紹給需要的同學,順便說下實現(xiàn)的原理
地址:ShellSnippet
檢測iOS主題資源包圖片大小的腳本
使用步驟
該腳本用于檢測iOS的資源包大小是否符合iOS的資源大小規(guī)范,防止因為圖標大小的不規(guī)范導致在不同屏幕的機型上體驗不一致。
使用步驟如下:
1、先把資源包解壓,資源文件會放在同一個目錄下,演示資源包存放的路徑是:
/Users/aron/GitRepos/iosdemos/YTThemeManagerDemo/Doocuments/IOS
2、然后執(zhí)行checkImage.sh腳本,如果提示沒有權限,使用命令chmod u+x ./checkImages.sh給腳本添加運行權限,會提示輸入目錄,復制粘貼第一步的目錄即可

3、執(zhí)行了腳本之后會輸出一個分析結果的文件
outLogFile.txt,該文件中保存了可能存在問題的資源文件列表
比如
nav_btn_share_black這個文件2x和3x的大小是一樣的,在3x的機器上顯示就會存在問題:圖標變小或者圖標的顯示效果變模糊,需要讓UI提供合適的圖標。

實現(xiàn)分析
實現(xiàn)步驟如下,主要使用了sips命令獲取圖片的信息,進行循環(huán)比較:
-
ls ${imagesFolder}命令獲取文件夾下的所有資源文件 -
sips -g pixelWidth ${imagePath}命令獲取圖片資源的信息 - 記錄上個圖片資源的信息,然后循環(huán)比較判斷圖片尺寸是否合適
對應的代碼如下,關鍵的地方有做了注釋了:
#/bin/sh
imagesFolder="/Users/aron/Desktop/CheckImages/IOS"
outpuLogFile="$(pwd)/outLogFile.txt"
echo "" > ${outpuLogFile}
# 輸入文件夾
echo -n "請輸入圖片目錄: "
read imagesFolder
if [[ -d $imagesFolder ]]; then
echo "圖片目錄正確"
else
echo -n "輸入的目錄無效,請重新執(zhí)行腳本"
fi
# 獲取下面的所有文件
lastHandleFile=""
lastHandleImageWidthHeight=""
for imageFullName in $(ls ${imagesFolder}); do
echo "imageFullName = ${imageFullName} lastHandleFile = ${lastHandleFile}"
// 使用sed的替換命令s,去掉圖片名稱中的 @2x.png @3x.png 結尾,方便比較
pureImageName=`echo ${imageFullName} | sed 's/@.*.png//g' | sed 's/\.png//g'`
if [[ ${pureImageName} == ${lastHandleFile} ]]; then
# 判斷大小
imagePath="${imagesFolder}/${imageFullName}"
// sips -g pixelWidth ${imagePath} 獲取到的結果如下:
// 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,默認轉(zhuǎn)換的源代碼是php和html結尾的源碼,需要轉(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)換,如果沒安裝,腳本會自動使用brew安裝enca命令,如果沒安裝brew,emmm。。。請自行百度安裝吧。
使用步驟
1、執(zhí)行命令,提示輸入源代碼的目錄,腳本會遍歷該目錄下的源文件,進行編碼轉(zhuǎn)換,結束。
實現(xiàn)分下
該腳本實現(xiàn)很簡單,主要是read_implement_file_recursively方法中處理遞歸遍歷,判斷文件的擴展名是否是php/html文件,符合要求的話使用enca命令轉(zhuǎn)換格式即可,使用的一些輔助方法包括
- 檢測命令是否安裝的方法
test_command_installed,如果未安裝,調(diào)用brew install enca進行安裝enca - 檢測文件加是否存在并提示輸入的方法
checkDirCore和checkInputDestDirRecursive
#!/bin/sh
# 檢測某個命令是否安裝
# @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)檢測輸入的文件夾
checkInputDestDirRecursive() {
echo -n "請輸入目錄: "
read path
if [[ -d $path ]]; then
CheckInputDestDirRecursiveReturnValue=$path
else
echo -n "輸入的目錄無效,"
checkInputDestDirRecursive
fi
}
# 檢測文件夾存在的方法,結果保存在全局變量`CheckInputDestDirRecursiveReturnValue`中
# 參數(shù)一:檢測的文件夾路徑
# 參數(shù)二:提示消息字符串
# 使用方式如下,去掉注釋
# # 導入工具腳本
# . ./FileUtil.sh
# # 檢測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 "處理目錄結束====="
else
# 文件
echo "處理文件 ${itemPath}"
// 判斷是否是.php/.html擴展名的文件
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:不是一個目錄"
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}