I have the following Python script to execute bash script and capture output.
#!/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")
This the test script.
#!/bin/bash
x=1
while [ $x -le 5 ]
do
printf "Hello World $x times\n"
x=$(( $x + 1 ))
sleep 3
done
Everything runs well, except whenever run is started a new output window will pop out. This behavior is different if the command is a system command (eg "ls -l"). It is very inconvenient as I like all the outputs in the same window.
Try shell=False
in process definition
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.