0

I am having a problem getting my python variable (1 file) to pass to my bash script. The python program pulls the file (csv file) and assigns it to var1 and then passes it to the bash script. The bash script uploads it to a server. The bash script works, but when I try to pass a file variable from python to bash, it does not work. It looks like the variable is empty and I get only the file name being loaded into the server, not the data. All the files are located in the same folder (upload.py, upload_files.sh, data_file.csv) I am new to this, so any help much appreciated.

python script

import subprocess

var1 = "data_file.csv"
subprocess.call(["bash", "upload_files.sh", var1])

BASH Script (partial bash script with changed info)

    #!bin/bash
    #
    export HOST=example_server.com

    export DATA_FILE=$1
    #
    curl -X PUT -L --header "Content-Type: text/csv" --data-binary@${DATA_FILE} "https://${HOST}:21/sserver/api/v3/fs/server-1/bulk_upload/${DATA_FILE}"
Mari
  • 11
  • 2
  • 1
    What if you replace the args array with `['bash upload_files.sh ' + var1]`? – Random Davis Nov 06 '20 at 19:44
  • Thank you, Just tried that, but unfortunately it produces as error "invalid syntax" – Mari Nov 06 '20 at 19:57
  • @Mari The error is in the `subprocess.call(['bash upload_files.sh ' + var1])` line? Can you share the full output? – AMC Nov 06 '20 at 21:49
  • The shebang is broken, it will try to look for `bash` in a subdirectory `bin` of the current directory. Though it's completely redundant if you call the script with `bash scriptname` anyway. (All of this code could usefully be in Python anyway.) – tripleee Jan 06 '21 at 15:23
  • @RandomDavis That's completely wrong; you have to pass in just a string, with `shell=True`, or a list of arguments, basically split on spaces. Perhaps see [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Jan 06 '21 at 18:10

2 Answers2

1

The var1="data_file.csv" the left hand side of the value of the variable was using a quote, variables are expanded if you are using speech marks.

If you want to use single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:

For example,

$ echo 'single quoted. '"Double quoted. "'Single quoted again.' single quoted. Double quoted. Single quoted again.

$ echo '"$name" has the value '"$name" "$name" has the value World

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • Sorry, just typos with the single quote and the double quote, fixed. Thanks for point out – Mari Nov 06 '20 at 21:10
0

You are missing a space after --data-binary and the quotes:

curl -X PUT -L --header "Content-Type: text/csv" --data-binary "@${DATA_FILE}" "https://${HOST}:21/sserver/api/v3/fs/server-1/bulk_upload/${DATA_FILE}"
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134