如何在 Linux 中的 bash 脚本中显示 GUI 消息框?
题
我正在 Ubuntu linux 下编写一些小 bash 脚本。我希望能够从 GUI 运行它们,而不需要终端窗口来输入任何输入或查看任何输出。
到目前为止,唯一需要的输入是 sudo 的密码 - gksudo 可以很好地处理这个问题。但我还没有找到一种简单的方法来显示消息框。是否有某种“gkmessage”命令可用?我更喜欢默认 Ubuntu 安装中存在的东西,但如果有必要,我不介意安装新的软件包。
其他提示
如果您正在使用 乌班图 许多发行版 notify-send
命令将在右上角抛出那些漂亮的易腐烂通知之一。就像这样:
notify-send "My name is bash and I rock da house"
美丽的!
每个人都提到禅宗,似乎还有很多其他的。一个混乱但有趣的列表位于 http://alternativeto.net/software/zenity/
首先是 zenity 的示例,其中包含文本格式标记、窗口标题、按钮标签。
zenity \
--info \
--text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nGet your <b>coffee</b>." \
--title="Coffee time" \
--ok-label="Sip"
广西留言
gxmessage "my text"
留言
xmessage
非常旧,所以它很稳定,并且可能在所有使用 X 的发行版中可用(因为它是与 X 一起发行的)。它可以通过 X 资源进行定制,对于那些已经使用 Linux 或 Unix 足够长的时间的人来说,知道它意味着什么(.Xdefaults
, , 任何人 ?)。
xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse "Is xmessage enough for the job ?" -timeout 10
对话框
(未测试)
在购电协议中
亚德:Zenity On Steroids [从 Shell 脚本显示图形对话框] ~ Web Upd8:Ubuntu/Linux 博客. 。似乎没有自动调整对话框大小。
echo My text | yad \
--text-info \
--width=400 \
--height=200
一个更大的例子
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 标准存储库中的
- 外壳图形用户界面
- 对话框
- gtk对话框
题外话(针对终端)
whiptail --msgbox "my text" 10 20
dialog --msgbox "my text" 10 20
请随意编辑。
这 禅宗 应用程序似乎就是您正在寻找的内容。
获取输入 禅宗, ,您可以指定一个变量并输出 禅宗--条目 保存到它。它看起来像这样:
my_variable=$(zenity --entry)
如果您查看中的值 我的变量 现在,它将是在 zenity 弹出输入对话框中输入的任何内容。
如果您想就用户(或您)应在对话框中输入的内容提供某种提示,请添加 - 文本 切换到您想要的标签。它看起来像这样:
my_variable=$(zenity --entry --text="What's my variable:")
Zenity 还有许多其他针对特定任务的不错选项,因此您可能还想使用 zenity--帮助. 。一个例子是 - 日历 选项可让您从图形日历中选择日期。
my_date=$(zenity --calendar)
它根据用户点击的内容给出了格式良好的日期:
echo ${my_date}
给出:
08/05/2009
还有滑块选择器、错误、列表等选项。
希望这可以帮助。
我找到了 留言 命令,这已经足够好了。
这是一个小 Tcl 脚本,可以完成您想要的操作。Ubuntu 上应该默认安装 Wish 解释器。
#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent
像这样称呼它:
myanswer=`gui-prompt "type your answer and press enter"`
alert
和 notify-send
似乎是同一件事。我用 notify-send
对于非输入消息,因为它不会窃取焦点,而且我找不到停止 zenity 等的方法。从这样做。
例如
# 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"
还有 dialog
和 KDE 版本 kdialog
. dialog
由 slackware 使用,因此它可能无法立即在其他发行版上可用。
Ubuntu 的怎么样 警报. 。它可以在任何操作后使用,以提醒其完成,如果操作完成时出现错误,甚至会显示红叉图标
ls -la; alert
如果没有其他东西存在。您可以启动 xterm 并在其中 echo,如下所示:
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);'
Kdialog 和dialog 都不错,但我推荐Zenity。xmessage 或对话框快速、简单且美观得多。