user3715398
user3715398

Reputation: 1

Shell script- CURL script returning error

This is getting a bit tricky. I have a shell script to create tasks in our JIRA. I'am running it on Ubuntu Server (a newbie).

I have a variable in the script as follows:

SCRIPT="curl -D- -u $USER:$PASSWORD -X POST --data @$SAMPLE_FILE -H \"Content-Type:   application/json\" $REST_URL"

I echo this script and run it in using $SCRIPT my shell script. When i bash my script, it always returns an error "curl: (6) Couldn't resolve host 'application'".

But if i try to run the printed SCRIPT(which i echoed) alone, it creates a task. I know it is a small problem but i'm not able to get it!

Any suggestions?

Upvotes: 0

Views: 1304

Answers (1)

that other guy
that other guy

Reputation: 123400

As suggested in the bash tag wiki, we can run your code through shellcheck to automatically check for common problems:

$ shellcheck yourscript

In yourscript line 1:
SCRIPT="curl... -H \"Content-Type: application/json\" $REST_URL"
       ^-- SC2089: Quotes/backslashes will be treated literally. Use an array.

In yourscript line 2:
$SCRIPT
^-- SC2090: Quotes/backslashes in this variable will not be respected.

Ok, then let's use an array:

command=(curl -D- -u "$USER:$PASSWORD" -X POST --data "@$SAMPLE_FILE" -H "Content-Type:   application/json" "$REST_URL")

# Print what we'll execute:
printf "%q " "${command[@]}"
echo

# Execute it:
"${command[@]}"

Upvotes: 2

Related Questions