从python调用shell脚本
我有一个python脚本,它调用一个shell脚本,然后调用一个名为iv4_console的.exe。为了调试,我需要打印iv4_控制台的标准输出。我用了这个: Python: 外壳:从python调用shell脚本,python,shell,Python,Shell,我有一个python脚本,它调用一个shell脚本,然后调用一个名为iv4_console的.exe。为了调试,我需要打印iv4_控制台的标准输出。我用了这个: Python: 外壳: start_dir=$PWD release=$1 echo Release inside shell: $release echo Directory: $start_dir cd $start_dir cd ../../iv_system4/ports/visualC12/Debug echo Debug d
start_dir=$PWD
release=$1
echo Release inside shell: $release
echo Directory: $start_dir
cd $start_dir
cd ../../iv_system4/ports/visualC12/Debug
echo Debug dir: $PWD
./iv4_console.exe ../embedded/LUA/analysis/verbose-udp-toxml.lua ../../../../../logs/$release/VASP_DUN722_20160307_Krk_Krk_113048_092_1_$release.dvl &>../../../../FCW/ObjectDetectionTest/VASP_DUN722_20160307_Krk_Krk_113048_092_1_$release.xml
./iv4_console.exe ../embedded/LUA/analysis/verbose-udp-toxml.lua ../../../../../logs/$release/VASP_FL140_20170104_C60_Checkout_afterIC_162557_001_$release.dvl &>../../../../FCW/ObjectDetectionTest/VASP_FL140_20170104_C60_Checkout_afterIC_162557_001_$release.xml
exit
但这不起作用,它什么也不打印。你觉得怎么样?你可以用
import subprocess
subprocess.call(['./c.sh'])
在python文件中调用shell脚本
或
请看我的评论,i.m.o的最佳方法是只使用python 但是,在回答您的问题时,请尝试:
import sys
import subprocess
var="rW015005000000"
proc = subprocess.Popen(["/bin/bash", "/full/path/to/c.sh"], stdout=subprocess.PIPE)
# Best to always avoid shell=True because of security vulnerabilities.
proc.wait() # To make sure the shell script does not continue running indefinitely in the background
output, errors = proc.communicate()
print(output.decode())
# Since subprocess.communicate() returns a bytes-string, you can use .decode() to print the actual output as a string.
您的Python代码将无法运行。请显示实际代码。很抱歉,我错过了RW015005000000左右的引号。那么,什么是proc?您是否尝试过proc=subprocess.Popen[c.sh,var]?不工作?我无法使用subprocess.call打印标准输出。使用shlex不起作用。请尝试导入操作系统。系统'shc.sh'
import subprocess
import shlex
subprocess.call(shlex.split('./c.sh var'))
import sys
import subprocess
var="rW015005000000"
proc = subprocess.Popen(["/bin/bash", "/full/path/to/c.sh"], stdout=subprocess.PIPE)
# Best to always avoid shell=True because of security vulnerabilities.
proc.wait() # To make sure the shell script does not continue running indefinitely in the background
output, errors = proc.communicate()
print(output.decode())
# Since subprocess.communicate() returns a bytes-string, you can use .decode() to print the actual output as a string.