<legend id='fDA6k'><style id='fDA6k'><dir id='fDA6k'><q id='fDA6k'></q></dir></style></legend>
        <bdo id='fDA6k'></bdo><ul id='fDA6k'></ul>
      <i id='fDA6k'><tr id='fDA6k'><dt id='fDA6k'><q id='fDA6k'><span id='fDA6k'><b id='fDA6k'><form id='fDA6k'><ins id='fDA6k'></ins><ul id='fDA6k'></ul><sub id='fDA6k'></sub></form><legend id='fDA6k'></legend><bdo id='fDA6k'><pre id='fDA6k'><center id='fDA6k'></center></pre></bdo></b><th id='fDA6k'></th></span></q></dt></tr></i><div id='fDA6k'><tfoot id='fDA6k'></tfoot><dl id='fDA6k'><fieldset id='fDA6k'></fieldset></dl></div>

      <small id='fDA6k'></small><noframes id='fDA6k'>

    1. <tfoot id='fDA6k'></tfoot>
      1. Python:使用参数(变量)执行shell脚本,但在shell脚本中未读取参数

        时间:2023-07-21

      2. <small id='MaI8C'></small><noframes id='MaI8C'>

              <i id='MaI8C'><tr id='MaI8C'><dt id='MaI8C'><q id='MaI8C'><span id='MaI8C'><b id='MaI8C'><form id='MaI8C'><ins id='MaI8C'></ins><ul id='MaI8C'></ul><sub id='MaI8C'></sub></form><legend id='MaI8C'></legend><bdo id='MaI8C'><pre id='MaI8C'><center id='MaI8C'></center></pre></bdo></b><th id='MaI8C'></th></span></q></dt></tr></i><div id='MaI8C'><tfoot id='MaI8C'></tfoot><dl id='MaI8C'><fieldset id='MaI8C'></fieldset></dl></div>
                  <tbody id='MaI8C'></tbody>
                <legend id='MaI8C'><style id='MaI8C'><dir id='MaI8C'><q id='MaI8C'></q></dir></style></legend>
                  <bdo id='MaI8C'></bdo><ul id='MaI8C'></ul>
                • <tfoot id='MaI8C'></tfoot>

                  本文介绍了Python:使用参数(变量)执行shell脚本,但在shell脚本中未读取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 python 执行一个 shell 脚本(不是命令):

                  main.py--------从子流程导入 Popen进程=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True)执行.sh----------echo $1//不打印任何东西echo $2//不打印任何东西

                  var1 和 var2 是我用作 shell 脚本输入的一些字符串.我错过了什么还是有其他方法可以做到这一点?

                  参考:如何使用子进程popen Python

                  解决方案

                  问题出在 shell=True 上.删除该参数,或将所有参数作为字符串传递,如下所示:

                  Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)

                  shell 只会将您在 Popen 的第一个参数中提供的参数传递给进程,就像它自己解释参数一样.查看此处回答的类似问题.实际发生了什么是你的 shell 脚本没有参数,所以 $1 和 $2 是空的.

                  Popen 将从 python 脚本继承 stdout 和 stderr,因此通常不需要向 Popen 提供 stdin=stderr= 参数(除非您运行脚本带有输出重定向,例如 >).只有在需要读取 python 脚本中的输出并以某种方式对其进行操作时,才应该这样做.

                  如果你只需要获取输出(并且不介意同步运行),我建议尝试 check_output,因为它比 Popen 更容易获取输出代码>:

                  output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])打印(输出)

                  请注意,check_outputcheck_call 对于 shell= 参数的规则与 Popen 相同.p>

                  I am trying to execute a shell script(not command) from python:

                  main.py
                  -------
                  from subprocess import Popen
                  
                  Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True)
                  
                  execute.sh
                  ----------
                  
                  echo $1 //does not print anything
                  echo $2 //does not print anything
                  

                  var1 and var2 are some string that I am using as an input to shell script. Am I missing something or is there another way to do it?

                  Referred: How to use subprocess popen Python

                  解决方案

                  The problem is with shell=True. Either remove that argument, or pass all arguments as a string, as follows:

                  Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)
                  

                  The shell will only pass the arguments you provide in the 1st argument of Popen to the process, as it does the interpretation of arguments itself. See a similar question answered here. What actually happens is your shell script gets no arguments, so $1 and $2 are empty.

                  Popen will inherit stdout and stderr from the python script, so usually there's no need to provide the stdin= and stderr= arguments to Popen (unless you run the script with output redirection, such as >). You should do this only if you need to read the output inside the python script, and manipulate it somehow.

                  If all you need is to get the output (and don't mind running synchronously), I'd recommend trying check_output, as it is easier to get output than Popen:

                  output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])
                  print(output)
                  

                  Notice that check_output and check_call have the same rules for the shell= argument as Popen.

                  这篇关于Python:使用参数(变量)执行shell脚本,但在shell脚本中未读取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python:非阻塞+非失效进程 下一篇:来自 subprocess.run() 的 CompletedProcess 不返回字符串

                  相关文章

                      <tfoot id='GQemC'></tfoot>
                    1. <i id='GQemC'><tr id='GQemC'><dt id='GQemC'><q id='GQemC'><span id='GQemC'><b id='GQemC'><form id='GQemC'><ins id='GQemC'></ins><ul id='GQemC'></ul><sub id='GQemC'></sub></form><legend id='GQemC'></legend><bdo id='GQemC'><pre id='GQemC'><center id='GQemC'></center></pre></bdo></b><th id='GQemC'></th></span></q></dt></tr></i><div id='GQemC'><tfoot id='GQemC'></tfoot><dl id='GQemC'><fieldset id='GQemC'></fieldset></dl></div>

                      <small id='GQemC'></small><noframes id='GQemC'>

                      • <bdo id='GQemC'></bdo><ul id='GQemC'></ul>
                      <legend id='GQemC'><style id='GQemC'><dir id='GQemC'><q id='GQemC'></q></dir></style></legend>