Skip to content Skip to sidebar Skip to footer

How To Pass An Array From Python To Shell Script And Execute It?

I have an array HSPACE which is defined inside a python script. I am trying to pass this array to a shell script 'arraytest' and execute it from python itself. I am trying with the

Solution 1:

The easy answer is to pass each array entry as a literal argv entry:

subprocess.call(['./arraytest'] + [str(s) for s in HSPACE], shell=False)

...thereafter...

#!/bin/bashprintf'hspace array entry: %q\n'"$@"

Another approach is to pass an array as a NUL-delimited stream on stdin:

p = subprocess.Popen(['./arraytest'], shell=False, stdin=subprocess.PIPE)
p.communicate('\0'.join(str(n) for n in HSPACE) + '\0')

...and, in your shell:

#!/bin/bash
arr=( )
while IFS= read -r -d '' entry; do
  arr+=( "$entry" )
doneprintf'hspace array entry: %q\n'"${arr[@]}"

Post a Comment for "How To Pass An Array From Python To Shell Script And Execute It?"