0

I am trying to include this curl command (to the Dropbox audit api) in a bash script:

curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXX' --header 'Content-Type: application/json' --data '{"time":{"start_time":"2018-07-16T18:07:56Z"}}'

I have replaced the auth token with XXXXX etc.

In my script the timestamp is actually a variable called $LAST.

I build up the curl command like this:

LAST=$(date +"%Y-%m-%dT%H:%m:%SZ")

DROPBOX1=$(echo 'curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header '\''Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'\'' --header '\''Content-Type: application/json'\'' --data '\''{"time":{"start_time":"')

DROPBOX2=$(echo '"}}'\''')

CURLSTRING=$DROPBOX1$LAST$DROPBOX2

echo $CURLSTRING
$CURLSTRING 

I build it up this way otherwise $LAST was problematic due to all the single and double quotes in the curl POST.

Running that small script gives this output:

curl -X POST https://api.dropboxapi.com/2/team_log/get_events --header 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXX' --header 'Content-Type: application/json' --data '{"time":{"start_time":"2018-07-16T18:07:56Z"}}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
100   175    0   127  100    48    232     87 --:--:-- --:--:-- --:--:--   231
curl: (6) Could not resolve host: Bearer
curl: (6) Could not resolve host: XXXXXXXXXXXXXXXXXXXXXXXXX'
curl: (6) Could not resolve host: application

As can been seen the echo command in the script shows what looks to be a completely valid curl command - indeed if I cut and past that output to the command line, it works. However, as the rest of the output shows in the bash script itself the curl command is not being interpreted properly.

I cannot for the life of me figure out why this is. Clearly echo is preserving the string correctly, whereas the curl command is breaking up the $CURLSTRING somehow. I am obviously not correctly using some combination of single and double quotes, but quite where is breaking is unclear.

thanks

Rob

robp1234
  • 1
  • 1
  • You can't safely store arbitrary lists (like a list of command-line arguments -- each of which is a separate string) in string variables (which, by nature, store exactly one string). Use an array, as given in [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy Jul 16 '18 at 17:31
  • ...which is to say -- this isn't specific to curl at all; unquoted expansion of *any* variable performs string-splitting and glob expansion, but **doesn't** parse quotes contained in that variable is if they were syntactic. This is necessary -- it would be impossible to write code that securely handled untrustworthy data in bash if that data could contain arbitrary syntax. – Charles Duffy Jul 16 '18 at 17:35
  • BTW, `foo=$(echo 'bar')` is a **really** inefficient way of writing `foo='bar'`. Command substitutions take multiple milliseconds to run even when they don't spawn external processes -- you're `fork()`ing off a subshell and capturing its output. – Charles Duffy Jul 16 '18 at 17:36
  • For `get_events`, are you sure you have to do POST. May be because you are passing the data `-d {...}`. run the whole command in a script and run the script via `bash -x yourscript.sh` and see how it'll expand your variables – AKS Jul 16 '18 at 18:04

0 Answers0