4

I'm creating a script that does a curl request to a predefined site. Unfortunately the script find the 3 parameters but the curl doesn't work properly.

Where is the problem? Here is my attempt.

random="$(cat something.txt)"
echo "ID: ${random} - File: $1 - Var: $2 - Cookie: $3"
url="$(curl -i -L -X POST --cookie 'info=$3' \
  -F 'var=$2' \
  -F 'submit=Send' \
  -F 'file[]=@$1' \
   https://example.com/upload?id=${random})"

The second line with echo print out the correct values and the same POST request does not give any problem using directly the parameters but the curl fails and gives me the following errors:

Warning: setting file /my/path/to.file  
Warning: failed!

1 Answer 1

4

Where is the problem?

You need to use double quotes.

Shellcheck will analyse your script and look for errors:

$ shellcheck myscript

Line 1:
random="$(cat something.txt)"
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.

Line 3:
url="$(curl -i -L -X POST --cookie 'info=$3' \
^-- SC2034: url appears unused. Verify use (or export if used externally).
                                   ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

Line 4:
  -F 'var=$2' \
     ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

Line 6:
  -F 'file[]=@$1' \
     ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

Line 7:
   https://example.com/upload?id=${random})"
                                 ^-- SC2086: Double quote to prevent globbing and word splitting.

$ 
5
  • Thank you for the suggestion, unfortunately I don't understant how to quote the file parameter \"file[]=@$1\" doesn't work
    – Timmy
    Commented Nov 2, 2018 at 21:38
  • 1
    @Timmy Why are you escaping the "s?
    – DavidPostill
    Commented Nov 2, 2018 at 21:40
  • Because the string start with url="$...
    – Timmy
    Commented Nov 2, 2018 at 21:44
  • Btw I tried to remove the escaping characters, now it works like a charm, thank you! I have to study more the quote resolution in bash :)
    – Timmy
    Commented Nov 2, 2018 at 21:49
  • @Timmy {shrug}. I don't know how to fix that.
    – DavidPostill
    Commented Nov 2, 2018 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .