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

本文介绍了一个使用Shell脚本解析配置文件并批量替换模板文件中特定字段的方法。涉及的技术点包括for循环、数据比较、if判断、文件替换及追加等。通过示例代码展示了如何读取输入文件中的数据,并根据不同的字段更新两个模板文件withdraw和deposit。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:我有两个模版文件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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值