在 curl POST 中扩展 shell 变量?
expanding shell variable in curl POST?
我使用以下行创建数据库:
curl -X POST 'http://10.1.1.1:8086/db?u=root&p=root' -d '{name: test1}
如果我尝试从 shell 脚本执行此操作:
ip=10.1.1.1
curl -X POST 'http://$ip:8086/db?u=root&p=root' -d '{name: test1}'
如果我尝试在双配额中使用它们,我在单配额内使用 shell 变量替换时遇到问题:
curl -X POST "http://$ip:8086/db?u=root&p=root" -d '{name: test1}'
变量扩展为正确的值,在终端打印
curl -X POST "http://10.1.21.1:8086/db?u=root&p=root" -d '{name: test1}': **No such file or directory**
这个问题的正确解决方案是什么?
试试这个:
ip=10.1.1.1
curl -X POST 'http://'"$ip"':8086/db?u=root&p=root' -d '{name: test1}'
我使用以下行创建数据库:
curl -X POST 'http://10.1.1.1:8086/db?u=root&p=root' -d '{name: test1}
如果我尝试从 shell 脚本执行此操作:
ip=10.1.1.1
curl -X POST 'http://$ip:8086/db?u=root&p=root' -d '{name: test1}'
如果我尝试在双配额中使用它们,我在单配额内使用 shell 变量替换时遇到问题:
curl -X POST "http://$ip:8086/db?u=root&p=root" -d '{name: test1}'
变量扩展为正确的值,在终端打印
curl -X POST "http://10.1.21.1:8086/db?u=root&p=root" -d '{name: test1}': **No such file or directory**
这个问题的正确解决方案是什么?
试试这个:
ip=10.1.1.1
curl -X POST 'http://'"$ip"':8086/db?u=root&p=root' -d '{name: test1}'