Shell脚本,更改Info.plist中的日期等


#!/bin/bash
root_src=$(dirname $(PWD)) bundle_name='RandomDebbot.bundle' target_path=$root_src/ecovacs/ecovacs/Vender/RobotSDK/Resource/$bundle_name echo $target_path rm -rf $target_path #强制删除目录命令rm -rf bundle_path=${TARGET_BUILD_DIR}/$bundle_name cp -R $bundle_path $target_path

上面是一个简单的不同工程中的资源文件copy脚本。

一)dirname - 截取给定路径的目录部分

shellPath=$(cd "$(dirname "$0")"; pwd)
echo $shellPaht

 二)basename -用于去掉文件名的目录和后缀

root_src=$(dirname $(PWD))
echo $root_src
AFNET_Lib_Path=$root_src/EcoRobotCoreLib/EcoRobotCoreLib/CommonLib/Utils/AFNetworking
AFNET_SDK_Path=$root_src/EcoRobotSDK/EcoRobotSDK/CommonLib/include/AFNetworking
find ${AFNET_Lib_Path} -name *.h -exec cp {} ${AFNET_SDK_Path} \;

 三) ${}、##和%%使用范例

介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。
假设定义了一个变量为:
代码如下:
file=/dir1/dir2/dir3/my.file.txt
可以用${ }分别替换得到不同的值:
${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt
${file#*.}:删掉第一个 . 及其左边的字符串:file.txt
${file##*.}:删掉最后一个 . 及其左边的字符串:txt
${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:删掉第一个 / 及其右边的字符串:(空值)
${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
记忆的方法为:
# 是 去掉左边(键盘上#在 $ 的左边)
%是去掉右边(键盘上% 在$ 的右边)
单一符号是最小匹配;两个符号是最大匹配
${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2
也可以对变量值里的字符串作替换:
${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt

四)用脚本来更换Info中的当前日期。
#!/bin/bash
root_src=$(dirname $(PWD))
echo ${root_src}
EcoRobotSDK_InfoPlist=${root_src}/test/Info.plist echo ${EcoRobotSDK_InfoPlist} # cat ${EcoRobotSDK_InfoPlist} Today=`date +"%2Y%m%d" ` #提取当前日期 echo ${Today} # echo ${Today:2:6} #从左边第几个字符开始,及字符的个数 170503
#按如下正则表达式要求搜索并替换为当前日期 sed
-i "" "s#<string>[0-9]\{4,\}<\/string>#<string>${Today:2:6}<\/string>#" ${EcoRobotSDK_InfoPlist}

 #/usr/libexec/PlistBuddy -c "Set :VersionDate ${Today:2:6}" ${EcoRobotSDK_InfoPlist}    #这个同上一句意思相同,而且效果更好

五) copyXcode资源文件的脚本

#!/bin/bash

#项目根目录
root_src=$(dirname $(PWD)) 
#CoreLib头文件目录
EcoCoreLib_Path=${root_src}/EcoRobotCoreLib;
#EcoRobotSDK中CoreLib头文件所在根目录
EcoRobotSDK_Header_Path=${root_src}/EcoRobotSDK/EcoRobotSDK/CommonLib
#AFNet_Lib_Path=${EcoCoreLib_Path}/EcoRobotCoreLib/CommonLib/Utils/AFNetworking

#找出CoreLib中的头文件
EcoCoreLib_Head_Files=`find ${EcoCoreLib_Path} -name "*.h"`
for file in $EcoCoreLib_Head_Files
do
    #取出名字
    filename=`basename ${file}`
    #找出头文件在EcoRobotSDK中的位置;如果头文件存在就copy过去
    SDK_Files=`find ${EcoRobotSDK_Header_Path} -name ${filename}`
    echo ${SDK_Files}
    if [[ -e ${SDK_Files} ]]; then
        cp -f ${file} ${SDK_Files}
    fi
done

 这个脚本有问题,  SDK_Files 其实是有值的,为空,所以它一直存在。所以还是每次都要copy

diff ${file} ${SDK_Files}
if [[ $0 != 0  &&  -e ${SDK_Files} ]]; 
    then
    cp -f ${file} ${SDK_Files}
fi
 $0 是上一执行命令的返回值
 diff 命令返回值为 0,说明两个文件相同, 否则两个文件不相同。

更改后的 

#项目根目录
root_src=$(dirname $(PWD))
#CoreLib头文件目录
EcoCoreLib_Path=${root_src}/EcoRobotCoreLib;
#EcoRobotSDK中CoreLib头文件所在根目录
EcoRobotSDK_Header_Path=${root_src}/EcoRobotSDK/Class/StaticLibaryHeaders
#AFNet_Lib_Path=${EcoCoreLib_Path}/EcoRobotCoreLib/CommonLib/Utils/AFNetworking

#目标文件夹不存在就退出,不执行
if [ ! -d ${EcoRobotSDK_Header_Path} ]; then
echo "目标文件夹不存在"
exit
fi
#源文件夹不存在就退出,不执行
if [ ! -d ${EcoCoreLib_Path} ]; then
echo "源文件夹不存在"
exit
fi

#找出CoreLib中的头文件
EcoCoreLib_Head_Files=`find ${EcoCoreLib_Path} -name "*.h"`
for file in $EcoCoreLib_Head_Files
do
#取出名字
filename=`basename ${file}`
#找出头文件在EcoRobotSDK中的位置;如果SDK中有这个头文件,且两个文件不一样就copy过去
SDK_Files=`find ${EcoRobotSDK_Header_Path} -name ${filename}`
#echo "${SDK_Files} 存在"
if [[ -n ${SDK_Files}  && -w ${SDK_Files} ]];     #前面判断字符串是否为空。
then
diff -bH ${file} ${SDK_Files}
if [ $? != 0 ]
then
cp -f ${file} ${SDK_Files}
fi
fi
done

 

http://blog.csdn.net/guo8113/article/details/39430335

 

六)删除项目中无用图片

#!/bin/sh  
PROJ=`find . -name '*.xib' -o -name '*.[mh]'`  
  
for png in `find . -name '*.png'`  
do  
    name=`basename $png`  
    if ! grep -qhs "$name" "$PROJ"; then  
        echo "$png is not referenced"  
    fi  
done  
-o 是或者的意思
-a 是而且的意思
-not 是相反的意思
#! /bin/bash
#组装的ImageName未检查出来
for i in `find . -name "*.png" -o -name "*.jpg"`; do
    imageName = 
    file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x | xargs basename -s @3x`
    result=`ack -i "$file"`
    # echo "result:     \n  $result"
    if [ -z "$result" ]; then
        echo "$i"
        echo "$i 疑似没有引用" >>image_delete.txt
        # 如果需要,可以直接执行删除:
        # rm "$i"
    fi
done

 

 
posted @ 2017-04-16 15:57  尘恍若梦  阅读(1924)  评论(1编辑  收藏  举报