接到需求的時(shí)候就用shell寫(xiě)了 , 結(jié)果4000個(gè)文件要檢測(cè)七八分鐘
然后用python實(shí)現(xiàn)了 ,只用了不到2秒........這差距
需求背景: 要檢查imageset里面2x圖和3x圖的缺失問(wèn)題
檢查指標(biāo):
1.imageset里面的Contents.json 里, 是否2x和3x都有對(duì)應(yīng)的filename
2.有filename的情況下,imageset里是否有對(duì)應(yīng)的圖片
畢竟不是專(zhuān)門(mén)寫(xiě)shell的 , 如果有更優(yōu)化的寫(xiě)法, 歡迎各位大佬指教
shell解析json比較麻煩 , 下面有2種 1.利用python 2.利用jq庫(kù)
brew install jq
直接上代碼:
#!/bin/sh
# find . -name '*.fb' -type f -print -exec rm -rf {} \;
############################# 分割線 [ 全局變量 ] ###################################
CHECK_PATH=$1 #要檢查的路徑
WHITELIST=$2 #白名單
echo 檢查路徑:${CHECK_PATH}
echo 白名單:${WHITELIST}
############################# 分割線 [ 自定義方法 ] ###################################
function check2x3x()
{
contents_path=$1
# echo $contents_path
# 1.查 @2x 和 @3x的 filename是否存在
# content=`cat $contents_path`
# lens=`echo $content| python3 -c "import sys, json; data = json.load(sys.stdin); print(len(data['images']))"`
# # echo $lens
# for (( j = 0; j < $lens; j++ )); do
# filename=`echo $content| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j].get('filename'))"`
# # echo $filename
# if [[ $filename == None ]]; then
# # echo "空"
# #filename 為空
# scale=`echo $content| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j]['scale'])"`
# echo "路徑: $contents_path ,Content.json文件缺少 @${scale} 圖片的 filename" >> checkImg_result.txt
# else
# # echo "非空"
# #filename 不為空 就檢查對(duì)應(yīng)圖片是否存在
# #拼圖片路徑
# imageset_dir=`dirname $contents_path`
# imgPath=${imageset_dir}/${filename}
# if [[ ! -f $imgPath ]]; then
# echo "路徑: ${imageset_dir} ,缺少 ${filename} 圖片" >> checkImg_result.txt
# fi
# fi
# done
# 1.查 @2x 和 @3x的 filename是否存在
content=`cat $contents_path`
lens=`echo $content | jq '.images' | jq length`
# echo $lens
# echo lens:$lens
for (( j = 0; j < $lens; j++ )); do
filename=`echo $content | jq ".images[${j}]" | jq '.filename'`
# echo $filename
if [[ $filename == None ]]; then
# echo "空"
#filename 為空
scale=`echo $content | jq ".images[${j}]" | jq '.scale'`
echo "路徑: $contents_path ,Content.json文件缺少 @${scale} 圖片的 filename" >> checkImg_result.txt
else
# echo "非空"
#filename 不為空 就檢查對(duì)應(yīng)圖片是否存在
#拼圖片路徑
imageset_dir=`dirname $contents_path`
imgPath=${imageset_dir}/${filename}
if [[ ! -f $imgPath ]]; then
echo "路徑: ${imageset_dir} ,缺少 ${filename} 圖片" >> checkImg_result.txt
fi
fi
done
# 2.查scale是否缺失 [不用做]
# 3.查對(duì)應(yīng)的圖片是否存在
# echo $contents
# lens=`cat $contents_path| python3 -c "import sys, json; data = json.load(sys.stdin); print(len(data['images']))"`
# echo 數(shù)組數(shù)量:$lens
# sizes=(1x 2x 3x)
# for (( j = 0; j < $lens; j++ )); do
# # echo $i
# size=`cat $contents_path| python3 -c "import sys, json; print(json.load(sys.stdin)['images'][$j]['scale'])"`
# # echo $size
# sizes=( ${sizes[*]/$size} )
# done
# echo "移除數(shù)組結(jié)束"
# # echo ${sizes[*]}
# for (( k = 0; k < ${#sizes[@]}; k++ )); do
# echo "路徑: $contents_path ,Content.json文件缺少 ${sizes[k]} 圖片" >> checkImg_result.txt
# done;
# echo 寫(xiě)入文件結(jié)束
}
############################# 分割線 [ 流程 ] ###################################
#判斷要檢查的路徑是否正確
if [[ ! -d $CHECK_PATH ]]; then
echo "\033[31m ERROR : 要檢查的不是一個(gè)路徑 , 程序退出 \033[0m"
exit 0
fi
#加載白名單 ,拼接查找命令
#有參數(shù),且存在
find_command_line="find $CHECK_PATH"
if [[ ! -z $WHITELIST ]] && [[ -f $WHITELIST ]]; then
echo 白名單存在
content=`cat whitelist.txt`
content=(${content})
for((i = 0; i < ${#content[@]}; i++)); do
echo 白名單目錄$i:${content[i]}
find_command_line="$find_command_line -path ${content[i]} -prune -o"
done;
find_command_line="$find_command_line -name *.imageset -type d -print"
else
find_command_line="find $CHECK_PATH -name *.imageset -type d"
fi
#刪除之前的記錄
if [ -f checkImg_result.txt ];then
echo "文件不存在"
rm checkImg_result.txt
fi
#獲取所有的 xxx.imageset
imageset_paths=`$find_command_line`
#轉(zhuǎn)換一下, 要不不是數(shù)組
imageset_paths=(${imageset_paths})
#遍歷路徑
contents_json_len=${#imageset_paths[@]}
echo 文件個(gè)數(shù):${#imageset_paths[@]}
for((i = 0; i < ${#imageset_paths[@]}; i++)); do
imageset_path=${imageset_paths[i]}
#拼接 Content.json路徑
contents_json_path=${imageset_path}/Contents.json
# echo Content路徑:$contents_json_path
#通過(guò)路徑檢查是否缺失@2x 或者 @3x 的圖片
check2x3x $contents_json_path
echo 進(jìn)度:$i/$contents_json_len
# echo "\n"
done;
open checkImg_result.txt