0

I am having some problem expanding a variable.

First I set the value of the key.

KEY=Basic YWRtaW46YWRtaW4=

Then I tried calling it in curl but it was not working. To diagnose the problem, I decided to print the actual command that was being run. This was the result

print curl -H `print "Authorization: " $KEY` "http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=0"

>> http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=0

It was only printing the string after the KEY. Whereas, If I replaced it myself, it was printing the correct output.

print curl -H "Authorization: Basic YWRtaW46YWRtaW4=" "http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=0" >> curl -H Authorization: Basic YWRtaW46YWRtaW4= http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=0

2
  • Please read (again) a Bash tutorial, this is very basic stuff and you should have find the solution before posting here on StackOverflow. Commented Mar 29, 2018 at 14:33
  • I am really sorry that this is a basic question. I will delete it very soon. Commented Mar 29, 2018 at 14:36

1 Answer 1

1

You need to enclose the value in quotes because you have a space.

KEY="Basic YWRtaW46YWRtaW4="

Doing KEY=Basic YWRtaW46YWRtaW4= without quotes is like assigning "Basic" to "KEY" and nothing to another variable called "YWRtaW46YWRtaW4".

Then simply use it like so:

curl -H "Authorization: $KEY" "http://192.168.1.1/userRpm/WlanMacFilterRpm.htm?Page=1&exclusive=0"
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, that seems to have fixed the main problem, however, I am getting the key from another command. KEY="$(curl -Ivs -X GET "http://admin:[email protected]" &> >(awk '/^> Authorization/{ print $3 " " $4 }'))" . How would I go about making it work with this?
You have to learn about process substitution, redirections and pipelines. Basically, you can assign the output of a command to a variable with VAR=$(command). Here your command is redirecting its output in a file that is created with the output of another command. This will not work. I think what you want is a pipeline: KEY="$(curl ... | awk ...)"
I need to pipe the stderr and not (necessarily) the stdout. So | did not work. I can assure you that the command curl -Ivs -X GET "http://admin:[email protected]" &> >(awk '/^> Authorization/{ print $3 " " $4 }') returns exactly the string that I want.
OK, my bad. But if your command works fine, then you have your key, so you have everything you need, no? Simply use the key in the second curl command :/ By the way, to pipe stdout + stderr, you can use |&.
No, unless you feel uncomfortable keeping it open.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.