Fla*_*Hyd 3 python shell python-2.7 python-3.x
我有一个要求用户输入的 shell 脚本。考虑下面的例子
测试文件
#!/bin/bash
echo -n "Enter name > "
read text
echo "You entered: $text"
echo -n "Enter age > "
read text
echo "You entered: $text"
echo -n "Enter location > "
read text
echo "You entered: $text"
Run Code Online (Sandbox Code Playgroud)
脚本执行:
sh test.sh
Enter name> abc
You entered: abc
Enter age > 35
You entered: 35
Enter location > prop
You entered: prop
Run Code Online (Sandbox Code Playgroud)
现在我在 python 程序中调用了这个脚本。我正在使用子流程模块执行此操作。据我所知,子流程模块创建了一个新流程。问题是当我执行 python 脚本时,我无法将参数传递给底层 shell 脚本,并且 scipt 处于 hault 阶段。能不能指出我做错的地方
python脚本(CHECK.PY):
import subprocess, shlex
cmd = "sh test.sh"
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout,stderr = proc.communicate()
print stdout
Run Code Online (Sandbox Code Playgroud)
Python 执行:check.py
python check.py
Run Code Online (Sandbox Code Playgroud)
您的代码正在运行,但由于您提到stdout=subprocess.PIPE内容将变为stdout您在stdout,stderr = proc.communicate(). stdout=subprocess.PIPE从您的Popen()调用中删除参数,您将看到输出。
或者,您应该使用subprocess.check_call()作为:
subprocess.check_call(shlex.split(cmd))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3809 次 |
| 最近记录: |