bash脚本编写-while循环不读取下一行并按预期存储值

时间:2019-04-29 02:39:11

标签: bash shell unix

我不知道为什么我的while循环结构中嵌入了这些if语句的情况不起作用。 我有一个名为 source.txt

的文件
mmsGroupId=5ab5c0e04eff45274ce5e471
mmsApiKey=5ab5c22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a

我希望我的bash脚本比较存储在配置文件(我使用sed replace)中的本地值(即mmsGroupId和mmsApiKey)。 最初,本地配置文件的键没有任何值,因此看起来像 test.config

mmsGroupId=
mmsApiKey=

如果键为空且不匹配,则将本地值替换为source.txt。

在某些情况下,如果mmsGroupId,mmsApiKey也将具有旧值,而我想用源文件中的值替换

mmsGroupId=123456789
mmsApiKey=123456789abcdefghijklmnopqrstuvwxyz

我正在使用while循环读取source.txt文件中的两行,但是我只得到第一行 (mmsGroupId),而不是(mmsApiKey)。

结果:

mmsGroupId=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a
mmsApiKey=5cc5e22c4eff45274ce5e63f07536eddebedec576b0e02904deb001a

期望:(请注意:此配置文件还有更多内容-我只是在显示期望的key = value)

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

2 个答案:

答案 0 :(得分:2)

我认为您应该根据循环中使用的key:value来区分操作,例如:

#!/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

答案 1 :(得分:1)

问题是您用错误的密钥进行了替换。在这个if语句中

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

应该在文件中的值不正确时运行else块,但在$keyname错误时也可以运行。

您可以使用嵌套的if语句或elif [[ "$keyname" = "mmsGroupId ]]来确保替换操作不会在错误的键上进行。

更好的解决方案是摆脱while循环和if语句:

for key in mmsGroupId mmsApiKey
do
  value=$(grep "$key" /tmp/source.txt | cut -d "=" -f2);
  sed -i "s/$key=.*/$key=$value/" /tmp/test.config
done