Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何在密码中转义双引号和感叹号?_Linux_Bash_Shell_Sh - Fatal编程技术网

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:确保安装了您需要的工具。