如何从命令行创建消息框?

Min*_*mus 104 command-line terminal

如何从命令行创建消息框,无论是 GUI 消息框还是终端内显示的消息框?

能够从用户那里获得简单的输入也很有趣,例如,通过单选按钮(是/否、确定等)给出的输入。

pol*_*lym 160

对于标准的“消息框”,请使用boxes

echo 'This is a test' | boxes
Run Code Online (Sandbox Code Playgroud)

boxes看起来像这样(第一个。第二个是像这样的自定义cowsay):

一个星号盒子和一个带有文本标志的 ASCII 艺术狗的屏幕截图


如果您的意思是警报框,请使用notify-send

notify-send 'title' 'message'
Run Code Online (Sandbox Code Playgroud)

notify-send 看起来像这样:

弹出消息阅读“你好 Ashframe...”


您还可以zenity用于弹出窗口:

zenity --error --text="An error occurred\!" --title="Warning\!"
Run Code Online (Sandbox Code Playgroud)

Zenity 更图形化并且有更多选项,比如让窗口显示为一个问题,使用:

zenity --question --text="Do you wish to continue/?"
Run Code Online (Sandbox Code Playgroud)

甚至进度条,使用:

find /usr | zenity --progress --pulsate --auto-close --auto-kill --text="Working..."
Run Code Online (Sandbox Code Playgroud)

zenity 看起来像这样:

带有按钮的错误、问题、信息和警告对话框


或使用dialog, 用于仅命令行的消息框:

dialog --checklist "Choose OS:" 15 40 5 \
1 Linux off \
2 Solaris on \
3 'HP UX' off \
4 AIX off
Run Code Online (Sandbox Code Playgroud)

dialog 看起来像这样:

带有 4 个选项的对话 TUI


另一种选择是whiptail

whiptail --title "Example Dialog" --msgbox "This is an example of a message box. You must hit OK to continue." 8 78
Run Code Online (Sandbox Code Playgroud)

whiptail 看起来像这样:

带有两个文本按钮的whiptail弹出框


如果你真的很疯狂,请使用toilet

toilet -F border -F gay "CRAZY"
Run Code Online (Sandbox Code Playgroud)

toilet 看起来像这样:

彩色文本框阅读“CRAZY”


Cir*_*郝海东 48

消息

这是 GUI 警报的鼻祖:

xmessage -center "Hello, World!"
Run Code Online (Sandbox Code Playgroud)

纯粹的复古善良。

我还打赌它应该在 X11 系统上广泛可用。

SO 线程:如何从 linux 中的 bash 脚本显示 GUI 消息框?| 堆栈溢出

在 Ubuntu 18.04 中测试。

  • `xmessage` 甚至默认安装在 Ubuntu 服务器上。所以我可以使用`ssh -Y <host>`在转发x个连接的同时登录机器,然后运行`some-command; xmessage "some-command is done"` 在长时间运行的命令完成时在我的桌面上弹出通知。 (2认同)
  • 在 4k 显示器上显得很小。 (2认同)

HBr*_*ijn 12

然后仅仅因为@polym 完全超出了顶级答案而错过了经典消息:

write <username> [<terminal>]- 向另一个用户发送消息。以交互方式或作为管道的一部分echo "message" | write username

写


和补充写,wall发送消息给所有用户

墙


not*_*bit 5

如果您愿意将文本通过管道传输到 Python 包装器,则可以使用terminaltables

pip3 install colorclass
pip3 install terminaltables
Run Code Online (Sandbox Code Playgroud)

然后在GitHub Repo 中,您可以使用其中一个示例编写 Python 包装器。

在此处输入图片说明