0

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?

2
  • How do you execute the command in $SCRIPT ? I don't recall it correctly, but I remember there were some caveats about how to execute something like that. You might also want to try exec "$SCRIPT" or exec $SCRIPT or $( $SCRIPT ). I need to test this when I am at home. Please also escape $SAMPLE_FILE and $USER:$PASSWORD using \" since you cannot be sure if they contain whitespaces. Commented Jun 6, 2014 at 14:42
  • Thank you for the input. I just ran it as $SCRIPT. But i just tried all of your suggestions(exec "$SCRIPT" or exec $SCRIPT or $( $SCRIPT ) ) but in vain. Or is there a better way to do it? Commented Jun 6, 2014 at 14:55

1 Answer 1

2

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[@]}"
Sign up to request clarification or add additional context in comments.

Comments

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.