将python输出馈送到whiptail
我希望通过将一些PYTHON代码的输出传递给“whiptail”,在无头linux服务器上使用TUI(文本用户界面)。不幸的是,whiptail似乎什么也没发生。当我通过管道从常规shell脚本输出时,whiptail工作正常。以下是我所拥有的: data-gen.sh将python输出馈送到whiptail,python,linux,bash,dialog,whiptail,Python,Linux,Bash,Dialog,Whiptail,我希望通过将一些PYTHON代码的输出传递给“whiptail”,在无头linux服务器上使用TUI(文本用户界面)。不幸的是,whiptail似乎什么也没发生。当我通过管道从常规shell脚本输出时,whiptail工作正常。以下是我所拥有的: data-gen.sh #!/bin/bash echo 10 sleep 1 echo 20 sleep 1 ... ... echo 100 sleep 1 $。/data-gen.sh|whiptail——标题“测试”——量规“量规”0 50
#!/bin/bash
echo 10
sleep 1
echo 20
sleep 1
...
...
echo 100
sleep 1
$。/data-gen.sh|whiptail——标题“测试”——量规“量规”0 50 0
我得到下面的进度条按预期递增
现在我尝试从python复制同样的东西: data-gen.py
#!/usr/bin/python
import time
print 10
time.sleep(1)
...
...
print 100
time.sleep(1)
$。/data-gen.py|whiptail--标题“测试”--量规“量规”0 50 0
我得到下面的进度条保持在0%。没有看到增量。一旦后台的python程序退出,Whiptail就会退出
有没有办法让python输出成功地通过管道传输到whiptail?我还没有尝试过对话;因为我想坚持使用whiptail,它是大多数ubuntu发行版上预装的
男人的鞭子说:
--仪表文本高度宽度百分比
A gauge box displays a meter along the bottom of the
box. The meter indicates a percentage. New percentages
are read from standard input, one integer per line. The
meter is updated to reflect each new percentage. If
stdin is XXX, the first following line is a percentage
and subsequent lines up to another XXX are used for a
new prompt. The gauge exits when EOF is reached on
stdin.
这意味着whiptail
读取标准输入。许多节目
通常在输出不进入文件时缓冲输出。强迫
python
要生成无缓冲输出,您可以:
- 使用
解除缓冲运行它
:
$ unbuffer ./data-gen.py | whiptail --title "TEST" --gauge "GAUGE" 0 50 0
- 在命令行上使用
-u
开关:
$ python -u ./data-gen.py | whiptail --title "TEST" --gauge "GAUGE" 0 50 0
- 修改
data-gen.py的shebang
:
#!/usr/bin/python -u
import time
print 10
time.sleep(1)
print 20
time.sleep(1)
print 100
time.sleep(1)
- 每次打印后手动刷新标准输出:
#!/usr/bin/python
import time
import sys
print 10
sys.stdout.flush()
time.sleep(1)
print 20
sys.stdout.flush()
time.sleep(1)
print 100
sys.stdout.flush()
time.sleep(1)
- 设置
pythonunbuffer
环境变量:
$ PYTHONUNBUFFERED=1 ./data-gen.py | whiptail --title "TEST" --gauge "GAUGE" 0 50 0
值得添加print 20;time.sleep(1)
到python
脚本。在我的机器上,data-gen.sh
使进度条逐渐移动,但data-gen.py
使进度条立即从0跳到100。尝试unbuffer./data-gen.py | whiptail--title“TEST”--gauge“gauge”0500
或python-u./data-gen.py | whiptail--title“TEST”--gauge“gauge”0500
。谢谢阿卡迪乌斯。。python-u./data-gen.py | whiptail。。。工作得很好。无论如何,要从python代码本身中取消缓冲输出,我将命令转换为应答。你可以在shebang中使用-u
/usr/bin/python-u修改。很好!!不知何故,在virtualbox中运行的ubuntu 16.04上,unbuffer不起作用。每次打印后手动冲洗似乎有点痛苦。因为-u工作得很好,所以我将使用它。您可以为此创建一个包装器函数。是的,我可以创建一个包装器。但是使用-u标志有缺点吗?缺点是您只能在shebang行中添加一个选项,但是您可以通过将选项(如-us
等)串联在一起来避免这个问题。好信息。再次感谢。这是我第一次遇到需要添加shebang行选项的情况。