bash - bash脚本-while循环不读取下一行并按预期存储值
问题描述
我无法弄清楚为什么我的带有这些嵌入式 if 语句的 while 循环构造不起作用。我有一个名为source.txt的文件
mmsGroupId=5ab5c0e04eff45274ce5e471
mmsApiKey=5ab5c22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
我希望我的 bash 脚本比较存储在配置文件(我使用 sed 替换)中的本地值(即 mmsGroupId 和 mmsApiKey)。最初,本地配置文件没有键值,所以它看起来像这个test.config。
mmsGroupId=
mmsApiKey=
如果键为空且不匹配,则将本地值替换为那些 source.txt。
如果 mmsGroupId、mmsApiKey 也将具有旧值并且我想用源文件中的内容替换它,也会出现这种情况
mmsGroupId=123456789
mmsApiKey=123456789abcdefghijklmnopqrstuvwxyz
我正在使用 while 循环来读取 source.txt 文件中的 2 行,但是我只得到第一行的 (mmsGroupId) 而不是 (mmsApiKey)。
结果:
mmsGroupId=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
mmsApiKey=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
期待:(注意:这个配置文件还有更多内容 - 我只是显示我期待的预期键 = 值)
mmsGroupId=5ab5c0e04eff45274ce5e471
mmsApiKey=5ab5c22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
到目前为止,我的脚本如下所示:
#!/bin/bash
set -x
KEY=/tmp/source.txt
IFS='='
while read keyname value;
do
echo "This is the mmsGroupId:$value"
curr_group=$(grep mmsGroupId /tmp/test.config | cut -d "=" -f2);
echo "This is the current mmsGroupId:$curr_group"
if [[ "$keyname" = "mmsGroupId" && "$value" = "$curr_group" ]]; then
echo "We have a match - $value:$curr_group - NOTHING TO DO"
else
echo "No match found - let us update it"
sed -i 's/mmsGroupId='"$curr_group"'/mmsGroupId='"$value"'/g' /tmp/test.config
fi
echo "This is the mmsApiKey:$value"
curr_apikey=$(grep mmsApiKey /tmp/test.config | cut -d "=" -f2);
echo "This is the current mmsApiKey:$curr_apikey"
if [[ "$keyname" = "mmsApiKey" && "$value" = "$curr_apikey" ]]; then
echo "We have a match - $value:$curr_apikey - NOTHING TO DO"
else
echo "No match found - let us update it"
sed -i 's/mmsApiKey='"$curr_apikey"'/mmsApiKey='"$value"'/g' /tmp/test.config
fi
done < "$KEY"
set +x
这是调试日志:
+ KEY=/tmp/OpsManagerKeys.txt
+ IFS==
+ read keyname value
+ echo 'This is the mmsGroupId:5bc5e0e04eff45274ce5e471'
This is the mmsGroupId:5bc5e0e04eff45274ce5e471
++ grep mmsGroupId /tmp/test.config
++ cut -d = -f2
+ curr_group=
+ echo 'This is the current mmsGroupId:'
This is the current mmsGroupId:
+ [[ mmsGroupId = \m\m\s\G\r\o\u\p\I\d ]]
+ [[ 5bc5e0e04eff45274ce5e471 = '' ]]
+ echo 'No match found - let us update it'
No match found - let us update it
+ sed -i s/mmsGroupId=/mmsGroupId=5bc5e0e04eff45274ce5e471/g /tmp/test.config
+ echo 'This is the mmsApiKey:5bc5e0e04eff45274ce5e471'
This is the mmsApiKey:5bc5e0e04eff45274ce5e471
++ grep mmsApiKey /tmp/test.config
++ cut -d = -f2
+ curr_apikey=
+ echo 'This is the current mmsApiKey:'
This is the current mmsApiKey:
+ [[ mmsGroupId = \m\m\s\A\p\i\K\e\y ]]
+ echo 'No match found - let us update it'
No match found - let us update it
+ sed -i s/mmsApiKey=/mmsApiKey=5bc5e0e04eff45274ce5e471/g /tmp/test.config
+ read keyname value
+ echo 'This is the mmsGroupId:5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a'
This is the mmsGroupId:5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
++ grep mmsGroupId /tmp/test.config
++ cut -d = -f2
+ curr_group=5bc5e0e04eff45274ce5e471
+ echo 'This is the current mmsGroupId:5bc5e0e04eff45274ce5e471'
+ echo 'This is the current mmsGroupId:5bc5e0e04eff45274ce5e471'
This is the current mmsGroupId:5bc5e0e04eff45274ce5e471
+ [[ mmsApiKey = \m\m\s\G\r\o\u\p\I\d ]]
+ echo 'No match found - let us update it'
No match found - let us update it
+ sed -i s/mmsGroupId=5bc5e0e04eff45274ce5e471/mmsGroupId=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a/g /tmp/test.config
+ echo 'This is the mmsApiKey:5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a'
This is the mmsApiKey:5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
++ grep mmsApiKey /tmp/test.config
++ cut -d = -f2
+ curr_apikey=5bc5e0e04eff45274ce5e471
+ echo 'This is the current mmsApiKey:5bc5e0e04eff45274ce5e471'
This is the current mmsApiKey:5bc5e0e04eff45274ce5e471
+ [[ mmsApiKey = \m\m\s\A\p\i\K\e\y ]]
+ [[ 5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a = \5\b\c\5\e\0\e\0\4\e\f\f\4\5\2\7\4\c\e\5\e\4\7\1 ]]
+ echo 'No match found - let us update it'
No match found - let us update it
+ sed -i s/mmsApiKey=5bc5e0e04eff45274ce5e471/mmsApiKey=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a/g /tmp/test.config
+ read keyname value
+ set +x
解决方案
我认为您应该根据循环中使用的键:值来区分操作,例如:
#!/bin/bash
function subst
{
echo "This is the $keyname:$value"
curr_value=$(grep $keyname test.config | cut -d "=" -f2);
echo "This is the current $keyname:$curr_value"
}
#set -x
KEY=source.txt
IFS='='
while read -r keyname value;
do
if [[ $keyname = 'mmsGroupId' ]]
then
echo "do group things function"
subst
else
echo "do apikey things function"
subst
fi
done < "$KEY"
添加替换功能可以简化代码的可重用性。
# bash test.sh
do group things function
This is the mmsGroupId:5ab5c0e04eff45274ce5e471
This is the current mmsGroupId:testconfiggroupvalueXXX
do apikey things function
This is the mmsApiKey:5ab5c22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
This is the current mmsApiKey:testconfigaplikeyvalueXXX
推荐阅读
- javascript - 如何仅基于路径的一部分重定向 url?
- php - 如何将位保存到sql中?
- python - 如何在我的 LSTM 算法中使用 batch_size?
- python - 使用带有 Flask 的 Application Factory 时如何在“app”上放置装饰器?
- python - pyspark json读取忽略空集
- javascript - 导出多个带参数的函数
- mysql - 如何在mysql usinf while循环中插入日期时间值
- flutter - 在 Dart 的主线程中强制执行
- python - 如何修复“列表索引超出范围”
- flutter - 当用户在谷歌地图上按下标记时如何显示 spinkit?(flutter_spinkit)