关于shell:Bash脚本卷曲

Bash script curl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

read -p 'Username: ' uservar
read -p 'Password: ' passvar

cacheVariable1=""Content-Type:application/json""
cacheVariable2=""Cache-Control:no-cache""
parametersVariable="'{"username":"$uservar","password":"$passvar"}'"
echo $parametersVariable
echo $cacheVariable1 $cacheVariable2
websiteVariable="https://example.com/session"

echo $websiteVariable

entireURL="curl -X POST -H"$cacheVariable1" -H"$cacheVariable2" -d"$parametersVariable""$websiteVariable""

echo"Entire URL IS: $entireURL"

result=`$entireURL`


echo"$result"

我想要这样的脚本:curl-x post-h"content-type:application/json"-h"cache-control:no-cache"-d'"username":"[email protected]","password":"password123""https://example.com/session"

整个URL是:curl-h"content-type:application/json"-h"cache-control:no-cache"-d'"username":"zzzzzz","password":"azzzsass""https://cloud.tenable.com/session

但它不会在bash中执行。它给了我这个错误:

1
{"statusCode":400,"error":"Bad Request","message":"child "username" fails because ["username" is required]","validation":{"source":"payload","keys":["username"]}}

但它不起作用。有人能帮我吗?

更新

我自己解决的。除了作为eval$entireurl执行之外,一切都是正确的。因为一个嵌入在括号中的命令作为子shell运行,所以我的环境变量丢失了。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

read -p 'Username: ' uservar
read -p 'Password: ' passvar

cacheVariable1=""Content-Type:application/json""
cacheVariable2=""Cache-Control:no-cache""
parametersVariable="'{"username":"$uservar","password":"$passvar"}'"
echo $parametersVariable
echo $cacheVariable1 $cacheVariable2
websiteVariable="https://example.com/session"

echo $websiteVariable

entireURL="curl -X POST -H"$cacheVariable1" -H"$cacheVariable2" -d"$parametersVariable""$websiteVariable""

echo"Entire URL IS: $entireURL"

result=`$entireURL`

eval $entireURL

这个很好用!