如何使用 shell 脚本在 运行 openshift 命令后获取特定列值的值?

How to get value of particular column value after running openshift command using shell script?

我 运行 在 openshift 平台的命令下生成一些输出

oc 获取 pods

姓名状态

工作 1 运行

job2 已完成

如何使用 shell 脚本从上述结果中仅提取状态并将其存储在变量中。

例如:状态=已完成

如果您将 --output=json 添加到您的命令中,您可以使用 JQ 来 select 状态。我发现 bash 脚本非常适合使用命令,但在解析输出时有很多缺点。使用 JSON 无论格式如何,您都可以 select 正确的密钥。

How to extract only status from the above result and store it in a variable using shell script.

ex : status=completed

尝试status=$(oc get pods -o=custom-columns=STATUS:.status.phase --no-headers)。您可以echo $status并查看保存在环境变量中的状态列表。