将 bash 脚本拆分为单独的命令,然后 运行 将它们一个接一个地执行
Split bash script into separate commands and run them them one-by-one
我希望能够 运行 来自 Python 的 bash shell 脚本(使用 subprocess.Popen
之类的东西),但显示在我的 GUI 上将与输出一起执行的命令在 stdout
和 stderr
中实时显示。 GUI 应显示 Input (newline) Output (newline) Input
等。我目前能够为单行 bash 命令实现它,但我需要一个更复杂的多行命令解析器。这是我的代码,仅适用于 bash
.
中的简单单行命令
test.sh:
ping -c 2 www.google.com
if [ "abc" = "ghi" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
我的 python 文件:
with open("test.sh", "r") as script:
for line in script:
if not line.strip().startswith('#') and not line.strip() == "":
print("Debug: Running " + line.strip() + " ...")
proc = subprocess.Popen(shlex.split(line), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = str(proc.stdout.readline().strip().decode()) + ""
err = str(proc.stderr.readline().strip().decode()) + ""
if output == '' and proc.poll() is not None:
print("Debug: Command completed...")
time.sleep(1)
break
if output:
# Code for updating a Gtk TextView buffer
GLib.idle_add(self.updateConsoleText, output + "\n")
if err:
# Code for updating a Gtk TextView buffer
GLib.idle_add(self.updateConsoleText, err + "\n")
不出所料,它不适用于涉及 if-else
、loops
、heredoc
s 等的多行代码。我正在寻找 bash
解析器至少可以识别命令何时结束,并且可以在使用多行命令的情况下将 bash 脚本拆分为单独的命令。
你觉得你能帮我找到这样的library/tool吗?
你可以使用trap命令。
这里有一个小例子来演示这个概念:
#!/bin/bash
# redirect stderr to a logfile
exec 2>/tmp/test.log
# print commands and arguments at execution
set -x
# set trap for every step, sleep one second.
# "sleep 1" could be every bash command
trap "sleep 1" DEBUG
echo -n "Name: "; read -r name
echo -n "Age: "; read -r age
echo "$name is $age years old"
与此脚本的 运行 并行,您可以使用 tail -f /tmp/test.log
来跟踪命令及其参数的调用。
我希望能够 运行 来自 Python 的 bash shell 脚本(使用 subprocess.Popen
之类的东西),但显示在我的 GUI 上将与输出一起执行的命令在 stdout
和 stderr
中实时显示。 GUI 应显示 Input (newline) Output (newline) Input
等。我目前能够为单行 bash 命令实现它,但我需要一个更复杂的多行命令解析器。这是我的代码,仅适用于 bash
.
test.sh:
ping -c 2 www.google.com
if [ "abc" = "ghi" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
我的 python 文件:
with open("test.sh", "r") as script:
for line in script:
if not line.strip().startswith('#') and not line.strip() == "":
print("Debug: Running " + line.strip() + " ...")
proc = subprocess.Popen(shlex.split(line), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = str(proc.stdout.readline().strip().decode()) + ""
err = str(proc.stderr.readline().strip().decode()) + ""
if output == '' and proc.poll() is not None:
print("Debug: Command completed...")
time.sleep(1)
break
if output:
# Code for updating a Gtk TextView buffer
GLib.idle_add(self.updateConsoleText, output + "\n")
if err:
# Code for updating a Gtk TextView buffer
GLib.idle_add(self.updateConsoleText, err + "\n")
不出所料,它不适用于涉及 if-else
、loops
、heredoc
s 等的多行代码。我正在寻找 bash
解析器至少可以识别命令何时结束,并且可以在使用多行命令的情况下将 bash 脚本拆分为单独的命令。
你觉得你能帮我找到这样的library/tool吗?
你可以使用trap命令。
这里有一个小例子来演示这个概念:
#!/bin/bash
# redirect stderr to a logfile
exec 2>/tmp/test.log
# print commands and arguments at execution
set -x
# set trap for every step, sleep one second.
# "sleep 1" could be every bash command
trap "sleep 1" DEBUG
echo -n "Name: "; read -r name
echo -n "Age: "; read -r age
echo "$name is $age years old"
与此脚本的 运行 并行,您可以使用 tail -f /tmp/test.log
来跟踪命令及其参数的调用。