首页 文章

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

提问于
浏览
126

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

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

12 回答

  • 7

    我相信Zenity会做你想要的 . 它's specifically designed for displaying GTK dialogs from the command line, and it'以Ubuntu package的形式提供 .

  • 44

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

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

    美丽!

  • 0

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

    首先,以文本格式标记,窗口 Headers ,按钮标签为特色的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

    gxmessage "my text"
    

    xmessage

    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
    

    kdialog

    (未测试)

    在PPA中

    YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog . 似乎没有自动调整大小对话框 .

    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标准存储库中

    • shellgui

    • xdialog

    • gtkdialog

    偏离主题(终端)

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

    随意编辑 .

  • 65

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

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

    my_variable=$(zenity --entry)
    

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

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

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

    Zenity有很多其他适用于特定任务的好选项,因此您可能希望使用 zenity --help 检查这些选项 . 一个例子是 --calendar 选项,让您从图形日历中选择日期 .

    my_date=$(zenity --calendar)
    

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

    echo ${my_date}
    

    得到:

    08/05/2009

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

    希望这可以帮助 .

  • 126

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

  • 2

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

    #!/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"`
    
  • 15

    alertnotify-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"
    
  • 2

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

  • 5

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

    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);'
    
  • 6

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

    ls -la; alert
    
  • 3

    Zenity确实是我认为您正在寻找的确切工具 .

    要么

    zenity --help
    
  • 125

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

相关问题