1

I have written several Bash scripts that use curl requests to upload multipart files to the server. If I call those scripts one by one, they all work as expected.

However, if I call them from the "root" script, that invokes them one by one, then my curl requests fail. This is the error I'm getting.

curl: (26) Failed to open/read local data from file/application

The script that uses curl to upload multipart file (normally works).

#!/bin/bash

curl --location --request POST 'localhost:8080/rest/v2/photos' \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer $user_login_token" \
--form 'image=@./photos-webp/photo-profile-2.webp' \
--form 'metadata=@./photos-webp/photo-profile-2-metadata.json;type=application/json'

The "root" script that invokes the curl script (yields the error).

#!/bin/bash

find . -print0 | while IFS= read -r -d '' file
do 
    if [[ $file == *.sh ]]; then 
        . $file
    fi
done

I don't understand why it normally works, but doesn't work when invoked from another script. How do I fix that?

1
  • An interesting approach. Not sure I can help, but you can bypass the if [[ ... test by telling find to return only .sh files, using find . -name '*.sh' -print0 ... . Good luck. Commented Jun 6, 2020 at 14:35

1 Answer 1

1

Your script which uses curl uses a variable ($user_login_token) which it does not set the value of. There will potentially be a difference in the value seen inside this script, depending how it was invoked.

  • If invoked directly from your login shell, using . filename or source filename, it will inherit the value set in the login shell (if any).

  • If invoked directly from your login shell, using something like ./filename or bash ./filename, it will be empty.

  • If invoked from your "root" script, then it will inherit the value from inside the root script. When the root script starts to execute, this similarly might or might not have inherited its value from your login shell, depending on how you invoked the root script (see above), but also, it can be overridden by any other script which the root script sources before it gets to the one which invokes curl. (Note that even if you invoke the root script using . filename or source filename, variables set inside the loop in the root script will not affect the value in your login shell after the root script finishes, because loops are executed inside a subshell, but it will affect the value seen in other scripts sourced from inside that loop.)

I suggest that you put echo $user_login_token inside both the loop in the root script and also the script that runs curl, and also put echo $file inside the loop in the root script, and hopefully from these you will be able to work out what it is set to in the two different cases, and where the value is being set.

The set -x command (for bash to display commands before it executes them, after any variable substitutions etc) is likely also to be useful in debugging this.

However, the safest approach is probably to run the script that runs curl using bash filename or ./filename so that it is self-contained, and then if it needs to source other scripts to set variables which it needs, then the appropriate . filename or source filename commands should be put explicitly inside the curl script itself. This will make it more robust.

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.