trying to use variable in bash-curl, getting curl: (3) <url> malformed - bash

we get error on trying to use variable in curl url block, how i can fix it?
variable is
NEXUS_UPLOAD='http://mynexus.com/nexus/content/repositories/releases/ru/123/project/1.0/1.0.289/'
#!/bin/bash -v
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" "${NEXUS_UPLOAD}")
echo $content
done
error is:
* <url> malformed
* Closing connection -1
curl: (3) <url> malformed
if we have direct link in this script, curl worked correctly
#!/bin/bash -v
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" http://mynexus.com/nexus/content/repositories/releases/ru/123/project/1.0/1.0.289/)
echo $content
done
using just $NEXUS_UPLOAD without quotes getting - curl: no URL specified!
also I try to use "$NEXUS_UPLOAD" in script, but still get malformed error

Remove the curly brackets around NEXUS_UPLOAD and use -x to enable the trace option, and don't forget to "export" the variables.
You can see a list of exported variables with declare -p
#!/bin/bash -x
for i in $(cat upload.list); do
content=$(curl -v -u "$NEXUS_USER":"$NEXUS_USER_PASSWORD" --upload-file "$i" "$NEXUS_UPLOAD")
echo $content
done
Here's a link explaining when they are useful.
For example:
i=image.jpg
convert $i ${i%jpg}png

Related

How i can safe the curl result in a bash variable

When i run the command
curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null on the normal command line i get the requested result {"success":false,"cause":"Token needed"}.
I need this result on a bash script but when i try to run it
curl = "$(curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null)"
echo $curl
I don't recieve the requested result i recieve this
[1/2]: "success":false --> <stdout>
--_curl_--"success":false
curl: (3) URL using bad/illegal format or missing URL
[2/2]: "cause":"Token needed" --> <stdout>
--_curl_--"cause":"Token needed"
curl: (3) URL using bad/illegal format or missing URL
How i can use the correct result in my bash script ?
Your command is not a variable assignment, it tries to executes curl with arguments = and the output of the command substitution. Remove the space characters before and after = and you may omit the quotes around the command substitution (this is one of the few occasions where quotes are not needed).
curl=$(curl -d "param1=value1&param2=value2" -X POST https://xxx.xxxx.de/xx/xx.php 2>/dev/null)
echo "$curl"

curl 400 bad request (in bash script)

I trying to do execute the following script in bash
#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH
if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7
else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi
But keep receiving a 400 bad request from the server.
When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.
The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.
edit 1 - hidding some sensitive content
edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)
The command i neet to input in server is:
curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7
And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.
I'm a bit new to the curl, any help can be apreciate.
Sorry for the bad english and tks in advance.

How to set command line argument using a variable in bash

I set a command line argument as below:
$TOKENARG='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H $(echo $TOKENARG) http://localhost:3001/api/v1/articles/3
And desired result is:
curl -v -X DELETE -H 'Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33' http://localhost:3001/api/v1/articles/3
But when I run the above one it doesn't work as expected and returns the following messages:
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: 9042f9a3caacc63419be489aefec02a5eae338c33
Could not resolve host: 9042f9a3caacc63419be489aefec02a5eae338c33
How should I pass argument using a variable?
Because you don't quote your command substitution, the string is split up into three words, Authorization:, Bearer and 9042f9a3caacc63419be489aefec02a5eae338c33. To prevent that, you could quote it:
curl -v -X DELETE -H "$(echo $TOKENARG)" http://localhost:3001/api/v1/articles/3
But you don't need echo and the command substitution at all: $(command) has the same output as command, so you can use
curl -v -X DELETE -H "$TOKENARG" http://localhost:3001/api/v1/articles/3
Side note: all uppercase variable names should be used for environment variables only, see for example the POSIX spec (fourth paragraph), so better would be
$tokenarg='Authorization: Bearer 9042f9a3caacc63419be489aefec02a5eae338c33'
curl -v -X DELETE -H "$tokenarg" http://localhost:3001/api/v1/articles/3

use curl in shell script to substitue a variable

Hi i am trying to use curl in shell script as shown below but i am not able to substitute the variable $line in the CURL . please suggest
while read line
do
echo "allowing mac $line"
curl -X POST -d '{"src-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json
curl -X POST -d '{"dst-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json
done < /home/floodlight/allowedmacs
No variable substitution in single quotes. Switch to double around the expansion, like this:
curl -X POST -d '{"src-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json
curl -X POST -d '{"dst-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json
Or you could use double-quotes around the whole thing and escape the inner ones:
curl -X POST -d "{\"src-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json
curl -X POST -d "{\"dst-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json
Either way, can't be inside single quotes when you get to $line if you want it expanded.

Run curl from .sh script with defined Content-Type

When I'm trying to run test.sh script I've always receive error from curl:
curl: (6) Couldn't resolve host 'application'
test.sh:
#!/bin/sh
CT="Content-Type:\ application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
But if I just run following command from console everything fine:
curl http://127.0.0.1 -H Content-Type:\ application/json
Could you please let me know what is wrong in script, as I understand something is wrong with 'space' escape, but have no idea how to fix it.
Also I've tried following combination, but result the same:
CT="Content-Type: application/json"
TEST="curl http://127.0.0.1 -H \"$CT\""
UPD:
bash / dash is only available on server. (/bin/sh --> bash)
GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)
Run the following: (delete the space after Content-Type)
#!/bin/bash
CT="Content-Type:application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
You can try with: bash -c your_bash_file.sh It worked for me with the same problem

Resources