How to run multiple commands in system, exec or shell_exec?

前端 未结 4 1056
小鲜肉
小鲜肉 2021-01-23 00:05

I\'m trying to run shell command like this from php:

ls -a | grep mydir

But php only uses the first command. Is there any way to force php to p

4条回答
  •  没有蜡笔的小新
    2021-01-23 00:50

    The answer:

    PLEASE avoid extensive solutions for such trivial things. Here it is the solution: *as it would be so long to do it in php, then do it in python (using subprocess.Popen in python would take three lines), and then call python's script from php.

    It's about seven lines in the end, and the problem ends up solved:

    Script in python, we'll call it pyshellforphp.py:

    import subprocess
    import sys
    comando = sys.argv[1]
    obj = subprocess.Popen(comando, stdout=subprocess.PIPE, stderr=subprocess.PIPE,   shell=True)
    output, err = obj.communicate()
    print output
    

    how to call the python script from php:

    system("pyshellforphp.py "ls | grep something");
    

提交回复
热议问题