0
#!/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"

I want my script like this: curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username":"[email protected]", "password":"Password123"}' "https://example.com/session"

Entire URL IS: curl -H "Content-Type:application/json" -H "Cache-Control:no-cache" -d '{"username":"zzzzzz","password":"azzzsass"}' https://cloud.tenable.com/session

But it does not execute in the bash. It gives me this error:

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

But it does not work. Can anyone help me?

Update


I solved it on my own. Everything was correct, except for executing as eval $entireURL. Because A command embedded within a parenthesis runs as a sub-shell so my environment variables were missing.

7
  • 2
    Please take a look: shellcheck.net Commented Feb 2, 2018 at 6:14
  • @Cyrus i checked, but i do not understand where i am going wrong. Can you help? Commented Feb 2, 2018 at 6:21
  • 1
    See BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!. Summary: don't put commands in variables, just execute them directly. (Capturing their output to variables is ok, though.) Commented Feb 2, 2018 at 6:35
  • @GordonDavisson I tried your way also but it did not work. thats why i went like this. It looks like there is some problem in the way i put curl script into bash, but i dont understand what. Commented Feb 2, 2018 at 6:42
  • 1
    I'd guess there's some problem with nested quotes in the -d parameter, but I'd have to see what you tried to be specific. In any case, putting it in a variable doesn't help any, it just adds another level of confusion. I'd post another question with the non-variable version. Commented Feb 2, 2018 at 6:48

1 Answer 1

4
#!/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

This works perfect !

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.