Shell command fails from python, ok from shell

前端 未结 2 1740
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 09:19

I have a python script that generates a number of shell commands from the given input. The problem is that when it tries to execute the generated commands, it fails, but when i

2条回答
  •  梦毁少年i
    2021-01-21 09:56

    I believe what you do is you start a single program called find with many parameters, incl. | and grep and xargs - and these are not arguments to find.

    What you want to do is probably to ask bash to run find and then pipe the outcome to grep, etc. One way to do that is to execute a single command called bash with two parameters (-c) and a whole string incl. piped commands, e.g.

    process = subprocess.Popen(["bash", "-c", "cat /etc/issue | grep a"], stdout=subprocess.PIPE)
    output=process.communicate()[0]
    print output
    

提交回复
热议问题