Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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

question from:https://stackoverflow.com/questions/66063209/trying-to-use-variable-in-bash-curl-getting-curl-3-url-malformed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...