如何在while循环中多次解析变量

时间:2018-07-24 22:35:21

标签: bash shell for-loop while-loop environment-variables

这是我的shell脚本,其中“ i”代表key.txt。密钥文件包含大约8个属性,其值由空格分隔,如下所示。

Key.txt文件:

bat slservice
solr slservice
tvs kimservice
ACM kimservice
product kimservice
tax kimservice
tvs kimservice
SNB taxservice

Shell脚本:

#!/bin/bash
while read i
do
    key=$(echo $i | awk '{print $1}')
    service=`echo $i | awk  '{print $2}'`
    ip=`cat IP.txt | grep $service | awk '{print $2}'|awk 'NR==1{print $1}'`

    echo "VALIDATING $service properties"
    echo "validating $key in $service IP = $ip"

    curl -X GET -H "Content-type: application/json" -H "Accept: application/json" http://${ip}/v1/config 2>/dev/null>$service.json

    v1_value=$(jq ".\"$key\"" "$service.json" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

    echo "$key $v1_value" > file.txt

    acutal_value=$(cat file.txt | grep "$key" | awk '{print $2}')
    LB_name=$(cat mapping.txt | grep "$key" | awk '{print $2}')
    LB_ip=$(cat LB.txt | grep "${LB_name}" | awk '{print $2}')

    if [ "${acutal_value}" == "${LB_ip}" ]; then
        echo "$key of $service value is matching $v1_value = $LB_ip"
    else
        echo "$key of $service v1/value ${acutal_value} is notmatching to GCP LB ${LB_ip}" 
    fi

done < key.txt

如果我们单独使用mapping.txt文件作为LB_name变量的输入文件,则上述脚本可以正常工作并给出确切的输出。

但是在我目前的情况下,我有4个映射文件(mapping1.txt到mapping4.txt),每个映射文件应只运行一次以执行密钥文件中的所有道具,然后再执行2,3,4个文件。

这是我们正在谈论的确切内容:

 LB_name=$(cat mapping.txt | grep "$key" | awk '{print $2}')

等待建议!

0 个答案:

没有答案