简体   繁体   English

传递多个参数的 Python 子进程无法按预期工作

[英]Python subprocess passing multiple arguments not working as expected

I have a bash file and passing values like this我有一个 bash 文件并传递这样的值

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在我的 bash 文件中,我得到了这样的值

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 .这里函数给出的值不正确,仅考虑“EAST US” 2 个参数。 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 + '"']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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