场景:
命令:
for i in `cat azuregateways.txt | awk -F "-00" {'print $1'}`; do newtoken=$(grep $i newtokens.csv | cut -d : -f2); az vm run-command invoke -g $i -n $i-00 --command-id RunShellScript --scripts """cp /home/ops/data/registrationticket.json /home/ops/data/registrationticket.json_old; sudo apt install -y jq; q --arg httpsas "$newtoken" '.OutboxAccessTicket.HttpSas=$httpsas' registrationticket.json_old > registrationticket.json; docker restart mygateway""";done
正在丢失的变量是$ newtoken。使用--debug执行该命令:
Command arguments: ['vm', 'run-command', 'invoke', '-g', 'ANDROIDGW', '-n', 'ANDROIDGW-00', '--command-id', 'RunShellScript', '--scripts', "cp /home/ops/data/registrationticket.json /home/ops/data/registrationticket.json_old; jq --arg httpsas 'ASDASD ASD ASD ASD ASD' '.OutboxAccessTicket.HttpSas=' /home/ops/data/registrationticket.json_old > /home/ops/data/registrationticket.json", '--debug']
因此,如果我们可以查看Comnmand参数部分,我们会注意到AZ CLI丢失了jq变量:
jq --arg httpsas'ASDASD ASD ASD ASD ASD''.OutboxAccessTicket.HttpSas ='
[我尝试将变量导出为全局变量,以各种可能的方式引用,执行脚本并运行该脚本,但是当我将标记作为变量引入时,它们的行为相同。
已经联系过Azure支持,他们建议使用字典来传递参数,因为毕竟az cli在后端运行py:
az vm run-command invoke -g $RGROUP -n $RGROUP-00 --command-id RunShellScript --scripts 'echo "$1" "$2"' --parameters "hello" "'this is just a test'"
{
"value": [
{
"code": "ProvisioningState/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"message": "Enable succeeded: \n[stdout]\nhello this is just a test\n\n[stderr]\n",
"time": null
}
]
}