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

前端 未结 13 2160
孤街浪徒
孤街浪徒 2020-12-04 04:58

I\'m writing a few little bash scripts under Ubuntu linux. I want to be able to run them from the GUI without needing a terminal window to enter any input or view any output

相关标签:
13条回答
  • 2020-12-04 05:38

    if nothing else is present. you can launch an xterm and echo in it, like this:

     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);'
    
    0 讨论(0)
  • 2020-12-04 05:40

    alert and notify-send seem to be the same thing. I use notify-send for non-input messages as it doesn't steal focus and I cannot find a way to stop zenity etc. from doing this.

    e.g.

    # 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"
    
    0 讨论(0)
  • 2020-12-04 05:40

    Zenity is really the exact tool that I think that you are looking for.

    or

    zenity --help
    
    0 讨论(0)
  • 2020-12-04 05:41

    I found the xmessage command, which is sort of good enough.

    0 讨论(0)
  • 2020-12-04 05:41

    How about Ubuntu's alert. It can be used after any operation to alert it finished and even show red cross icon if operaton was finnished with errors

    ls -la; alert
    
    0 讨论(0)
  • 2020-12-04 05:42

    The zenity application appears to be what you are looking for.

    To take input from zenity, you can specify a variable and have the output of zenity --entry saved to it. It looks something like this:

    my_variable=$(zenity --entry)
    

    If you look at the value in my_variable now, it will be whatever was typed in the zenity pop up entry dialog.

    If you want to give some sort of prompt as to what the user (or you) should enter in the dialog, add the --text switch with the label that you want. It looks something like this:

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

    Zenity has lot of other nice options that are for specific tasks, so you might want to check those out as well with zenity --help. One example is the --calendar option that let's you select a date from a graphical calendar.

    my_date=$(zenity --calendar)
    

    Which gives a nicely formatted date based on what the user clicked on:

    echo ${my_date}
    

    gives:

    08/05/2009

    There are also options for slider selectors, errors, lists and so on.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题