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

被折叠的 条评论
为什么被折叠?



