当前位置:首页> 正文

如何在linux中的bash脚本中显示GUI消息框?

如何在linux中的bash脚本中显示GUI消息框?

How to show a GUI message box from a bash script in linux?

我正在Ubuntu linux下编写一些小的bash脚本。 我希望能够从GUI运行它们,而无需终端窗口输入任何输入或查看任何输出。

到目前为止,唯一需要的输入是sudo的密码 - 而gksudo处理得很好。
但我还没有找到一种简单的方法来显示消息框。 是否有某种"gkmessage"命令可用? 我更喜欢默认的Ubuntu安装中存在的东西,但我不介意在必要时安装新的包。


如果您使用 Ubuntu 许多发行版,notify-send命令将在右上角抛出其中一个漂亮的易腐通知。像这样:

notify-send"My name is bash and I rock da house"

美丽!


我相信Zenity会做你想做的事。它专门用于从命令行显示GTK对话框,它可以作为Ubuntu包使用。


每个人都提到了禅宗,似乎还有很多其他人。一个混乱但有趣的列表在http://alternativeto.net/software/zenity/

首先,以文本格式标记,窗口标题,按钮标签为特色的zenity示例。

1
2
3
4
5
6
7
zenity \
--info \
--text="<span size="xx-large">Time is $(date +%Hh%M).</span>

Get your coffee."
\
--title="Coffee time" \
--ok-label="Sip"

gxmessage

1
gxmessage"my text"

xmessage

xmessage非常老,所以它很稳定,可能在所有使用X的发行版中都可用(因为它随X一起发布)。它可以通过X资源进行自定义,对于那些已经使用Linux或Unix足够长的人来了解它的含义(.Xdefaults,任何人?)。

1
xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse"Is xmessage enough for the job ?" -timeout 10

kdialog

(未测试)

在PPA中

YAD:Zenity On Steroids [显示Shell脚本的图形对话] ~Web Upd8:Ubuntu / Linux博客。似乎没有自动调整对话框大小。

1
2
3
4
echo My text | yad \
--text-info \
--width=400 \
--height=200

一个更大的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yad \
--title="Desktop entry editor" \
--text="Simple desktop entry editor" \
--form \
--field="Type:CB" \
--field="Name" \
--field="Generic name" \
--field="Comment" \
--field="Command:FL" \
--field="Icon" \
--field="In terminal:CHK" \
--field="Startup notify:CHK""Application""Name""Generic name""This is the comment""/usr/bin/yad""yad" FALSE TRUE \
--button="WebUpd8:2" \
--button="gtk-ok:0" \
--button="gtk-cancel:1"

其他人不在Ubuntu标准存储库中

  • shellgui
  • xdialog
  • gtkdialog

偏离主题(终端)

1
2
whiptail --msgbox"my text" 10 20
dialog --msgbox"my text" 10 20

随意编辑。


zenity应用程序似乎是您正在寻找的。

要从zenity获取输入,您可以指定一个变量并将zenity --entry的输出保存到它。它看起来像这样:

1
my_variable=$(zenity --entry)

如果你现在查看my_variable中的值,它将是zenity弹出输入对话框中输入的内容。

如果要提示某些用户(或您)应该在对话框中输入的内容,请添加带有所需标签的--text开关。它看起来像这样:

1
my_variable=$(zenity --entry --text="What's my variable:")

Zenity有很多其他适合特定任务的好选项,所以你可能想用zenity检查那些 - help。一个例子是--calendar选项,让您从图形日历中选择日期。

1
my_date=$(zenity --calendar)

根据用户点击的内容,它提供了格式良好的日期:

1
echo ${my_date}

得到:

08/05/2009

滑块选择器,错误,列表等也有选项。

希望这可以帮助。


我找到了xmessage命令,这有点好。


alertnotify-send似乎是一回事。我使用notify-send作为非输入消息,因为它没有窃取焦点,我找不到阻止zenity等的方法。

例如

1
2
3
4
5
# This will display message and then disappear after a delay:
notify-send"job complete"

# This will display message and stay on-screen until clicked:
notify-send -u critical"job complete"


这是一个小Tcl脚本,可以做你想要的。应该在Ubuntu上默认安装Wish解释器。

1
2
3
4
5
#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent

像这样称呼它:

1
myanswer=`gui-prompt"type your answer and press enter"`

还有dialog和KDE版本kdialog。 slackware使用dialog,因此可能无法立即在其他发行版上使用。


如果没有别的东西存在。你可以启动一个xterm并在其中回显,如下所示:

1
 xterm -e bash -c 'echo"this is the message";echo;echo -n"press enter to continue"; stty sane -echo;answer=$( while ! head -c 1;do true ;done);'

Ubuntu的警报怎么样?它可以在任何操作后用于提醒它完成,如果operaton有错误,甚至可以显示红叉图标

1
ls -la; alert

Zenity真的是我认为你正在寻找的确切工具。

要么

1
zenity --help


Kdialog和对话都很好,但我推荐Zenity。快速,简单,更好地查看xmessage或对话框。


展开全文阅读

相关内容