Linux 如何在密码中转义双引号和感叹号?
我有以下代码:Linux 如何在密码中转义双引号和感叹号?,linux,bash,shell,sh,Linux,Bash,Shell,Sh,我有以下代码: curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl 在上面的curl命令中,我想对密码中的“和!进行转义。现在,密码的形式是$2 我修改了curl命令,如下所示,但它似乎不起作用 curl -s --insecure -H "Content-Type: application/json
curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl
在上面的curl
命令中,我想对密码中的“
和!
进行转义。现在,密码的形式是$2
我修改了curl命令,如下所示,但它似乎不起作用
curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl
不要试图手动生成JSON;使用像
jq
这样的程序,它知道如何正确地转义,为您生成JSON
json=$(jq -n --arg u "$1" --arg p "$2" '{username: $u, password: $p}')
curl -s --insecure -H "Content-Type: application/json" -X POST -d "$json" http://apiurl
它是如何工作的?错误?不是您期望的输出?在
$1
和$2
中有什么?谢谢。但是我有大约100台服务器,我将在其中上载此脚本。如果其中一些服务器上没有安装jq怎么办?您需要使用JSON:确保安装了您需要的工具。