我知道从技术上讲,它可能会按原样工作,并且这是人们所期望的bash
语言,但这不是我所期望和写的。
我试图让一切尽可能简单。
This is fileA
:
+--------------------+-----------+------------+-----------+------------+
| Name | Status | Networks | Image | Plans |
+--------------------+-----------+------------+-----------+------------+
| HostName A | PAUSED | IP=1.1.1.1 | Ubuntu20 | Plan BGP40 |
| An other hostname | ACTIVE | IP=2.2.2.2 | Ubuntu20 | Plan BGP30 |
| final.destination | REBOOTING | IP=3.3.3.3 | Debian11 | Plan BGP10 |
| no problem | ACTIVE | IP=4.4.4.4 | Centos7 | Plan BGP90 |
| no problem | ACTIVE | IP=6.6.6.6 | Ubuntu22 | Plan BGP60 |
+--------------------+-----------+------------+-----------+------------+
This is fileB
:
dedicated_ip: 1.1.1.1
name: 'Plan BGP40'
dedicated_ip: 2.2.2.2
name: 'Plan BGP30'
dedicated_ip: 3.3.3.3
name: 'Plan BGP10'
dedicated_ip: 4.4.4.4
name: 'Plan BGP90'
dedicated_ip: 5.5.5.5
name: 'Plan BGP20'
dedicated_ip: 6.6.6.6
name: 'Plan BGP100'
所有IP都存在于fileA
应该存在于fileB
,但是 IP 地址在fileB
不能出现在fileA
,所以如果 IP 就可以了5.5.5.5
上表中不存在。
这是我的 bash 脚本:
while read -r ips_file_a
do
while read -r plans_file_a
do
while read -r ips_file_b
do
while read -r plans_file_b
do
if [ "$ips_file_a" == "$ips_file_b" ]
then
if [ "$plans_file_a" == "$plans_file_b" ]
then
echo "$ips_file_a == $ips_file_b"
echo "$plans_file_a == $plans_file_b"
fi
fi
done < <(awk '{ if ( $1 ~ /name/ ) { print $2, $3 } }' fileB | sed -e "s|'||g") # print plans and then remove single quote
done < <(awk '{ if ( $1 ~ /dedicated_ip/ ) { print $2 } }' fileB) # print IPs
done < <(awk -v d="Plans" 'BEGIN{FS="[[:blank:]]*[|][[:blank:]]*"; idx=0} FNR==2{for(i=1; i<=NF; i++) if (d==$i) idx=i; next} NR>1 {print $idx}' fileA | awk 'NF') # to print only Plans and then remove empty lines
done < <(awk -v d="Networks" 'BEGIN{FS="[[:blank:]]*[|][[:blank:]]*"; idx=0} FNR==2{for(i=1; i<=NF; i++) if (d==$i) idx=i; next} NR>1 {print $idx}' fileA | awk 'NF') # to print only IPs and then remove empty lines
这是当前的输出:
1.1.1.1 == 1.1.1.1
Plan BGP40 == Plan BGP40
2.2.2.2 == 2.2.2.2
Plan BGP40 == Plan BGP40
3.3.3.3 == 3.3.3.3
Plan BGP40 == Plan BGP40
4.4.4.4 == 4.4.4.4
Plan BGP40 == Plan BGP40
我尝试改变顺序while
循环,但我要么得到上面的输出,要么得到以下输出:
1.1.1.1 == 1.1.1.1
Plan BGP40 == Plan BGP40
1.1.1.1 == 1.1.1.1
Plan BGP30 == Plan BGP30
1.1.1.1 == 1.1.1.1
Plan BGP10 == Plan BGP10
1.1.1.1 == 1.1.1.1
Plan BGP90 == Plan BGP90
# then four times for each IP with four different plan names
我当前的输出有问题。我设置set -x
并看到在 IP 和计划相等后,它再次继续使用该 IP 的循环。例如,如果 IP1.1.1.1
和计划Plan BGP10
匹配,则继续检查是否1.1.1.1 == 2.2.2.2
并且该计划还Plan BGP10 == Plan BGP30
举个例子,它需要更多的时间并且不会退出if
条款为真。
这是预期的输出:
1.1.1.1 == 1.1.1.1
Plan BGP40 == Plan BGP40
2.2.2.2 == 2.2.2.2
Plan BGP30 == Plan BGP30
3.3.3.3 == 3.3.3.3
Plan BGP10 == Plan BGP10
4.4.4.4 == 4.4.4.4
Plan BGP90 == Plan BGP90
Update 1
The fileA
包含例如serverA
有这些虚拟机。
The fileB
是我的网站的输出,我的客户根据他们的计划付费。
我想检查客户端的IP是否1.1.1.1
确实在使用Plan BGP40
或不。假设客户支付了网站升级费用,Plan BGP90
,但我的网站无法连接到serverA
,所以升级没有发生。
假设在上面的示例中,该客户付费购买了更大的计划,但仍在使用旧的计划。或者甚至客户正在为较小的计划付费,但实际上正在使用更大的计划。
我想检查一下是否存在此类问题。
Update 2
我添加了IP6.6.6.6
在两个文件中。现在这位客户正在支付Plan BGP100
但他的服务器资源是Plan BGP60
。所以,他为这个大计划付出了代价,但事实上他的资源并没有BGP60
.
这是一个问题的例子。但在预期的输出中,IP6.6.6.6
将不会被打印。稍后我会找到那些没有打印的IP。