如何在 shell 脚本中的 curl 命令中传递变量

您所在的位置:网站首页 curl加参数 如何在 shell 脚本中的 curl 命令中传递变量

如何在 shell 脚本中的 curl 命令中传递变量

2024-06-16 20:38| 来源: 网络整理| 查看: 265

问题: 在shell中使用curl 调用接口时,在-d{} 中传递了变量,但是变量没有有效的传值:

具体现象如下:

#!/bin/sh cat pl_superId.txt|while read line do superId=$line echo $superId curl 'http://superid-authorize-uat.immotors.com/superid-authorize/authorize/bindingBmId' -H "Content-Type:application/json" -X POST -d '{"superId": "${superId}"}' done

结果执行时都会报错 {“status”:500,“error”:“30001”,“timestamp”:1655273713292,“error_description”:“服务内部异常”} 原因是变量superId在curl中没传值,变成了curl ‘http://superid-authorize-uat.immotors.com/superid-authorize/authorize/bindingBmId’ -H “Content-Type:application/json” -X POST -d ‘{“superId”: “”}’

解决办法

在curl中引用变量时要加个转义 “'” ,即一个双引号+一个单引号+一个双引号 修改脚本如下:

#!/bin/sh cat pl_superId.txt|while read line do superId=$line echo $superId curl 'http://superid-authorize-uat.immotors.com/superid-authorize/authorize/bindingBmId' -H "Content-Type:application/json" -X POST -d '{"superId": "'"${superId}"'"}' done

再次执行,成功传参了: 请添加图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3