简体   繁体   中英

Python subprocess passing multiple arguments not working as expected

I have a bash file and passing values like this

param1="EAST US"
param2="WEST US"
param3="NORT US"

p = subprocess.Popen(
    ['bash', '-c', '. /root/kickstart_test.sh;'
                   ' myfun ' + param1 + ' ' + param2 + ' ' + param3]

and In my bash file I am getting these values like this

myfun()
{
  echo $1  # output is  EAST
  echo $2  # output is  US
  echo $3  # output is  WEST
}

here function giving values not correct just considering "EAST US" 2 parameters . how I can fix this ?

The passed parameters need quoting and so:

p = subprocess.Popen(
['bash', '-c', '. /root/kickstart_test.sh;'
               ' myfun "' + param1 + '" "' + param2 + '" "' + param3 + '"']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM