bash while 循环没有按预期工作

2024-04-11

我知道从技术上讲,它可能会按原样工作,并且这是人们所期望的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。


假设/理解:

  • 基于连接两个文件fileA/IP == fileB/dedicated_ip and fileA/Plans - fileB/name
  • 如果 ip 和计划/名称完全匹配,则打印所需的输出

虽然这种类型的处理可以在bash,它将需要匹配来自FileA反对pairs行数来自fileB...可行,但有点复杂。由于当前的代码已经包含了六个awk脚本,我们可以让我们的生活变得更加简单(并且代码将是lot效率更高)如果我们只使用一个awk脚本来完成所有事情。

One awk idea:

awk -v qt="'" '
                # 1st file

FNR==NR         { if (FNR>=4 && !($0 ~ /-------/)) {                     # only look at lines from FNR>=4 and ignore any lines with "--------------"
                     split($0,a,"[[:blank:]]*[|][[:blank:]]*")           # split on "|" with possible leading/trailing white space; a[4] will contain the "IP=a.b.c.d" string and a[6] will contain the plan name
                     split(a[4],b,"=")                                   # split the "IP=a.b.c.d" string; b[2] will contain the ip address
                     plans[b[2]] = a[6]                                  # save our data: plans[ip]="plan name"
                  }
                  next
                }

                # 2nd file

/dedicated_ip:/ { ip = $2 }                                              # save the ip address
/name:/         { split($0,c,qt)                                         # split the line on a single quote; c[2] will contain plan name
                  if (ip in plans && c[2] == plans[ip])                  # if current ip is an index in plans[] array, and current plan name matches the plan name stored in plans[ip] ...
                     printf "%s == %s\n%s == %s\n", ip, ip, c[2], c[2]   # print the desired output
                }
' fileA fileB

这会生成:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

bash while 循环没有按预期工作 的相关文章

随机推荐

  • 发布到 Nexus 上的私有 NPM 存储库时出现身份验证错误

    我在发布到托管在我的私人 Nexus 上的私人 npm 注册表时遇到身份验证问题 我的 Nexus 设置是npm 代理 npm 注册表 托管 npmallowRepublish false npm 快照 托管 npmallowRepubli
  • Matplotlib Axes3D.quiver 图中箭头的长度不同

    有什么方法可以改变箭袋图中各个箭头的长度吗 我使用以下方法创建了一个绘图 lines to draw list of numpy 3d vectors xs ys zs list of coordinates us vs ws list o
  • 即使应用程序处于非活动状态,CursorLoader 如何自动更新视图?

    我一直在开发一个小型待办事项列表应用程序 我使用 CursorLoader 从内容提供商更新 ToDolistview 我写了一个函数onNewItemAdded 当用户在文本视图中输入新项目并单击 Enter 时调用 参考如下 publi
  • 在同一 pandas 数据框中交换两行(连同索引)

    我正在制定固定转售价格dataset https data gov sg dataset resale flat prices 如果您有兴趣 我使用的是 2015 年 1 月以后的数据 First I group the data by u
  • 单击警报消息中的“确定”后如何关闭浏览器中的当前窗口?

    基于页面中的某些操作 我想向用户发出警报消息 即 您的简历已上传 当用户在该警报框中单击 确定 时 我想关闭该窗口 我想仅使用警报方法而不是 JavaScript 中的确认方法来执行此操作 为什么 因为警报方法给出了唯一的选项 确定 而确认
  • MOVE-TO 期望输入是代理,但得到的是 NOBODY

    我的代码所做的是设置一个内部灰色补丁区域和一个外部黑色补丁区域 海龟可以在其中繁殖 每个补丁上有一个 一旦乌龟到达灰色和黑色区域之间的边界 我就会分配可变能量 以将乌龟的繁殖延迟一定的刻度 每个刻度能量增长一个单位 当能量达到一定数量时 我
  • 无法编译使用 boost 中的 odeint 的 C++

    我使用的是 Ubuntu 12 04 并且 usr include 中已经有一些 boost fies 我做了一个 sudo apt get install libboost all dev 并且还安装了很多文件 我不想删除这个 boost
  • 多个 JSlider 相互反应始终等于 100%

    我正在尝试向 java swing 应用程序添加 3 个 JSlider 以便三个滑块的总价值总和为 100 每个滑块都是一个概率 滑块 A 是将值添加到队列的概率 滑块 B是从队列中删除某个值的概率 滑块 C 是什么都不发生的概率 示例
  • img 标签中的 SVG 未在 Firefox 中作为图像加载

    我正在尝试使用 img 标签加载我的 svg 文件 但它在 Firefox 上不起作用 Chrome 显示 svg 我正在尝试这样做http www schepers cc svg blendups embedding html http
  • matplotlib 颜色条不起作用(由于垃圾收集?)

    我有一个与此类似的绘图功能 def fct f figure ax f add subplot 111 x y mgrid 0 5 0 5 z sin x 2 y 2 ax pcolormesh x y z 当我在中定义上面的函数时ipyt
  • iPhone 应用程序:应用程序进入 App Store 之前在特定设备上进行 Beta 测试

    我在App程序门户中注册了2台设备 只有我有一台 Mac 和设备才能下载该应用程序进行测试 另一个用户没有 Mac 但他有一部 iPhone 其他用户是否可以下载该应用程序进行测试 以便我们可以在将该应用程序发布到 App Store 上供
  • 根据日期条件之间另一列中的条件计算唯一文本值

    我需要的是根据 FIAP Medium Year 列的标准计算 TITLE 中唯一值的公式 这需要首先查看工作表 M Year 列中的日期 范围为2013年3月23日 2016年6月1日 然后需要检查 I FIAP Medium 列来寻找
  • AWS Lambda 与依赖项打包

    进一步概述是在 NodeJS 和 Monorepo 基于 Lerna 的背景下进行的 我有 AWS 堆栈 其中通过 AWS CloudFormation 部署了多个 AWS Lambda 一些 lambda 很简单 单个小模块 并且可以内联
  • iPhone/iPad:如何以编程方式获取屏幕宽度?

    您好 我想知道是否有办法以编程方式获取宽度 我正在寻找足够通用的东西来容纳 iphone 3gs iphone 4 ipad 此外 宽度应根据设备是纵向还是横向 对于 ipad 进行更改 有人知道该怎么做吗 我已经找了一段时间了 谢谢 看一
  • NHibernate:无法解析继承的 id 属性

    我定义了以下实体 public class Foo Entity
  • C# 裁剪然后缩放裁剪后的图像

    我正在尝试构建此类 在 ASP NET 站点中使用 它将裁剪给定自定义宽度 高度 X Y 的图像 然后获取结果图像并将其缩放为自定义宽度 高度 并保存在服务器返回该图像的 url 我将在查询字符串中获取这些参数 如下所示 Default a
  • 在python中将标题写入excel文件

    如何循环遍历列表中的每个元素并将其作为 Excel 标题 如果有重复的问题 请告诉我 到目前为止我还没找到 row 0 col 0 j 0 title No Hue Saturation Value Lightness AComponent
  • 过滤 ObservableCollection?

    当我将 ListBox 直接绑定到 ObservableCollection 时 我会在 ListBox 中显示实时更新 但是一旦我在混合中添加其他 LINQ 方法 我的 ListBox 就不再收到 ObservableCollection
  • 如何告诉 CPAN make 和 cc 的路径

    使用 opencsw org 软件包在 Solaris 上运行 Perl 5 10 CPAN 软件包中的 Makefile PL 无法找到正确的路径和 cc gcc 我找到了make的路径并将其设置为gmake 但我找不到cc的任何设置 我
  • bash while 循环没有按预期工作

    我知道从技术上讲 它可能会按原样工作 并且这是人们所期望的bash语言 但这不是我所期望和写的 我试图让一切尽可能简单 This is fileA Name Status Networks Image Plans HostName A PA