首页 > 解决方案 > 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

标签: bashshellunix

解决方案


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

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

推荐阅读