【问题标题】:Azure CLI echo command not respondingAzure CLI 回显命令没有响应
【发布时间】:2017-12-13 18:27:51
【问题描述】:

我希望将我的连接字符串存储在以下变量中:

connectionString= az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv

当我执行上述操作时,我会在响应中获得完整的连接字符串。但是,当我输入:

echo $connectionString

...我得到一个空白的回复。变量没有被存储。还有什么可以尝试的建议吗?

【问题讨论】:

    标签: azure azure-storage azure-cli


    【解决方案1】:

    您可以使用command substitution 来捕获变量中的输出:

    connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
    

    如果您需要跨多行保留输出,即当 Azure CLI 返回 JSON 格式的值时,您可能希望使用稍微不同的格式输出到 stdout。

    考虑这个例子:

    varResourceGroup=$(az group show -n $resourceGroup)
    

    使用与示例中相同的命令输出到 stdout 将产生一行:

    echo $varResourceGroup 
    { "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>", "location": "westeurope", "managedBy": null, "name": "<resourceGroup_name>", "properties": { "provisioningState": "Succeeded" }, "tags": null }
    

    如果您使用明显不同的格式,则会保留换行符:

    echo "$varResourceGroup"
    {
      "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>",
      "location": "westeurope",
      "managedBy": null,
      "name": "<resourceGroup_name>",
      "properties": {
        "provisioningState": "Succeeded"
      },
      "tags": null
    }
    

    【讨论】:

    • 请提供更多解释,说明您的答案是做什么的以及它为什么有效。虽然这可能会立即对 OP 有所帮助,但更详细的解释将证明对未来的其他用户更有帮助。
    • 谢谢@Engineero,你是对的 - 我添加了更多信息。
    • 太棒了!感谢您的快速响应和抱歉迟到。这有效!
    【解决方案2】:

    正如 Holger 所说,我们可以使用这个脚本来定义变量:

    connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
    

    另外,我们可以用这种方式来定义这个变量,像这样:

    [root@jasoncli@jasonye ~]# connectionstring=`az storage account show-connection-string -n jasondisk3 -g jasonauto --query connectionString -o tsv`
    [root@jasoncli@jasonye ~]# echo $connectionstring
    DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=jasondisk3;AccountKey=m+kQwLuQZiI3LMoMTyAI8KxxxxD+ZaT9HUL3Agxxxxqul4s8fAIHGPMTD/AG2j+TPHBpttq5hXRmTaQ==
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多