通过linux shell实现按模版生成配置功能

背景:我有两个模版文件widthdraw和deposit,另外有一个input,需要解析input的配一行配置数据,然后按不同字段来替换widthdraw和deposit文件

涉及到的shell技术点:需要for循环,数据比较,if判断,文件替换,文件追加


详细代码如下:

rm -rf withdraw.sql
rm -rf deposit.sql

touch withdraw.sql
touch deposit.sql

>withdraw.sql
>deposit.sql

ruleId=10010000

cat input.txt | while read line
do
     echo "==============================="
     IFS=',' arr=($line)
     i=0
     cp withdraw_temp.sql withdraw_temp.sql.bak &&
     cp deposit_temp.sql deposit_temp.sql.bak &&

     sed -i "s/RULE_ID_TEMP/${ruleId}/g" deposit_temp.sql.bak  &&
     let ruleId+=1

     sed -i "s/RULE_ID_TEMP/${ruleId}/g" withdraw_temp.sql.bak
     let ruleId+=1

     for x in ${arr[@]}; do
        if [ $i == 0 ];then
           echo "userId      ="$x
	   sed -i "s/USER_ID_TEMP/${x}/g" withdraw_temp.sql.bak &&

	   sed -i "s/USER_ID_TEMP/${x}/g" deposit_temp.sql.bak
        elif [ $i == 1 ];then
           echo "inAccount   ="$x
	   sed -i "s/ACCOUNT_NO_TEMP/${x}/g" withdraw_temp.sql.bak

        elif [ $i == 2 ];then
           echo "outAccount  ="$x
	   sed -i "s/ACCOUNT_NO_TEMP/${x}/g" deposit_temp.sql.bak
        elif [ $i == 3 ];then
           echo "signId      ="$x
	   sed -i "s/SIGN_ID_TEMP/${x}/g" withdraw_temp.sql.bak &&

	   sed -i "s/SIGN_ID_TEMP/${x}/g" deposit_temp.sql.bak
        elif [ $i == 4 ];then
           echo "instId      ="$x
	   sed -i "s/INST_ID_TEMP/${x}/g" withdraw_temp.sql.bak &&

	   sed -i "s/INST_ID_TEMP/${x}/g" deposit_temp.sql.bak
        elif [ $i == 5 ];then
           echo "instDetail  ="$x
	   sed -i "s/INST_DETAIL_TEMP/${x}/g" withdraw_temp.sql.bak &&

	   sed -i "s/INST_DETAIL_TEMP/${x}/g" deposit_temp.sql.bak
        else
	   echo "*****************error*****************"
	fi
        let i+=1
     done

     cat withdraw_temp.sql.bak >> withdraw.sql &&
     rm -rf withdraw_temp.sql.bak &&

     cat deposit_temp.sql.bak >> deposit.sql &&
     rm -rf deposit_temp.sql.bak
done


你可能感兴趣的:(linux开发环境)