【发布时间】:2022-09-28 00:21:46
【问题描述】:
我有以下 Python 脚本来执行 bash 脚本并捕获输出。
#!/usr/bin/env python3
import subprocess
def run(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
while True:
line = process.stdout.readline().rstrip()
if not line:
break
print(line)
run(\"hello_world.sh\")
run(\"hello_world.sh\")
run(\"hello_world.sh\")
这是测试脚本。
#!/bin/bash
x=1
while [ $x -le 5 ]
do
printf \"Hello World $x times\\n\"
x=$(( $x + 1 ))
sleep 3
done
一切运行良好,除非每次运行开始时都会弹出一个新的输出窗口。如果命令是系统命令(例如 \"ls -l\"),则此行为会有所不同。这很不方便,因为我喜欢同一窗口中的所有输出。
标签: python python-3.x bash git-bash