一个用于导出android项目配置宏为单个独立xml文件的shell脚本
最近由于工作需要将android项目的项目配置文件里面的宏文件导出来,项目配置宏文件内容如下所示:
GEMINI=yes GOOGLE_RELEASE_RIL=no HAVE_AACENCODE_FEATURE=yes HAVE_AEE_FEATURE=yes HAVE_APPC_FEATURE=no HAVE_AWBENCODE_FEATURE=yes HAVE_CMMB_FEATURE=no HAVE_GROUP_SCHEDULING=no
现在需要将这些宏导出来,由大写字母改成小写字母,类似于如下的xml文件格式:
<?xml version="1.0" encoding="utf-8"?> <permissions> <feature name="mtk_fm_recording_support" /> </permissions>
#!/bin/bash # # Description: # Used to export the ProjectConfig.mk items to different xml files. # # History: # tcpipstack, Oct.,29th, 2012, created it. # tcpipstack, Oct.,30th, 2012, modified for add the read LINE and process it. # #-------------------------------------------------------------------------------------------- DATE=`date +%F-%H` # FN_EXEC_RESULT=0 FN_EXEC_RESULT_SUCCESS=0 FN_EXEC_RESULT_ERR=1 # orginal project config file SRC_PRJ_CFG_FILE=/work/code/linux/shell/PrjCfgExportXml/ProjectConfig.mk # destination project config file. DST_PRJ_CFG_FILE=/work/code/linux/shell/PrjCfgExportXml/Dst_PrjCfg.mk # SRC_FILE_NAME=${SRC_PRJ_CFG_FILE} DST_FILE_NAME=${DST_PRJ_CFG_FILE} #-------------------------------------------------------------------------------------------- # if unexpect event happens, it will process here. function fn_check_error() { echo " fn_check_error..." exit=$? echo "exit = ${exit}" if [ ${exit} != 0 ] ; then echo " Error code ${exit}" fn_exit ${exit}; fi } # sub function exit with error code. function fn_exit() { local fn_exit_code=${1} exit ${fn_exit_code} } # print the global parameters function fn_print_global_params() { echo -e "\n fn_print_global_params..." echo "--------YES_OR_NO : ${YES_OR_NO}" } # filter LINEs include the string of yes or no. function fn_filter_special_LINE() { echo -e "\n fn_filter_special_LINE..." if [[ "${YES_OR_NO}" == "yes" ]]; then echo "${YES_OR_NO} == yes" sed -n '/yes\|YES/s/\([^=]\+\).*/\L\1/p' ${SRC_FILE_NAME} > ${DST_FILE_NAME} elif [[ "${YES_OR_NO}" == "no" ]]; then echo "${YES_OR_NO} == no" sed -n '/no\|NO/s/\([^=]\+\).*/\L\1/p' ${SRC_FILE_NAME} > ${DST_FILE_NAME} else fn_exit ${FN_EXEC_RESULT_ERR} fi fn_check_error } # read every LINE and process function fn_while_read_LINE() { echo -e "\n fn_while_read_LINE..." cat ${DST_FILE_NAME} | while read LINE do echo "$LINE" fn_process_LINE_items $LINE : done } # process every LINE function fn_process_LINE_items() { echo " fn_process_LINE_items: ${1}" local CFG_ITEM=${1} fn_write_cfg_2_xml ${CFG_ITEM} } # write the project config information to xml file function fn_write_cfg_2_xml() { echo " fn_write_cfg_2_xml: ${1}" local XML_FILE_NAME=${1} echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" > ${XML_FILE_NAME} echo "<permissions>" >> ${XML_FILE_NAME} echo " <feature name=\"${1}\" />" >> ${XML_FILE_NAME} echo "</permissions>" >> ${XML_FILE_NAME} mv ${XML_FILE_NAME} ${XML_FILE_NAME}.xml } #-------------------------------------------------------------------------------------------- echo -e "\n========================Start...========================\n" date if [ $# != 1 ];then echo "ERR: U must input one parameter: yes or no. Exit!" fn_exit ${FN_EXEC_RESULT_ERR} else YES_OR_NO=${1} fi fn_print_global_params fn_filter_special_LINE fn_while_read_LINE echo -e "\n========================End! ========================\n"
上述脚本里面最关键的就是这条语句,参考sed的用法可以参考这篇博客:http://blog.csdn.net/tcpipstack/article/details/8125819:
sed -n '/yes\|YES/s/\([^=]\+\).*/\L\1/p' ${SRC_FILE_NAME} > ${DST_FILE_NAME}
使用方法,在终端上输入如下:
bash prj_cfg_export_xml.sh yes/no