Azure CLI 从 table 中选择一个选项

Azure CLI choose a option from a table

我正在尝试在 Azure CLI 中获取带有数字的 table 输出,它将此作为输出

Number      Location     Name
----------  -----------  -------------
1           somewhere    ResourceGroup1
2           somewhere    ResourceGroup2

我现在的密码是

az group list --query '[].{location:location, name:name}'

我现在得到的输出是

Location     Name
----------  ---------------
somewhere    ResourceGroup1
somewhere    ResourceGroup2

我的最终目标是,如果您选择数字 1,您就可以 select 名称,这样我可以稍后在脚本中使用它

据我了解,您正在尝试创建变量以便稍后从输出中使用。你不需要先把它放在 table 中。使用相同的示例,您可以执行以下操作;

gpname="$(az group list --query [0].name --output tsv)"

az group show -n $gpname

祝你好运......

评论中的信息::

您正在寻找的比 Azure 更 Linux。我不是 Linux CLI 专家,但她是您可以构建的基本脚本。

#!/bin/bash

gpnames="$(az group list --query [].name --output tsv)"

PS3='Select A number: '

select gpname in $gpnames

do

az group show -n $gpname

Done

希望对您有所帮助......

对于您的问题,没有任何Azure CLI 命令可以实现它。但是您可以使用脚本来实现它。例如,您可以使用 shell 脚本:

#!/bin/bash

az group list --query '[].{location: location, name: name}' -o table >> output.txt

# This command just add the line number inside the file, it's optional.
cat -n output.txt >> result.txt

# you can just get the group name with a specific line, the same result with output.txt
awk '{if (NR == line) print }' result.txt 

希望这会有所帮助。

可以在过滤器中使用contains表达式(jmespath)来过滤结果:

filter=resource_group_name
filterExpression="[?contains(name, '$filter')].name"
az group list --query "$filterExpression" -o tsv

与现有答案相比,这是一种更好的方法。

更多阅读:
http://jmespath.org/specification.html#filterexpressions
http://jmespath.org/specification.html#built-in-functions