6

I'm currently writing a nemo action script in bash, and I need a way to get input from the user.

How?, the terminal isn't showing when running an action script.

is there anyway to popup a query window in the GUI to ask the user for input?

2
  • 1
    Which shell is using it?
    – Mreyes
    Aug 9 '20 at 21:37
  • 1
    @Mareyes what do you mean?
    – JoBe
    Aug 10 '20 at 1:41
9

Zenity is a good tool for that.

user_input=$(zenity --entry)

That assigns to variable user_input whatever the user types in the GUI window, unless the user presses cancel, in which case the exit code is not zero.

user_input=$(zenity --entry)
if [ $? = 0 ]; then 
    echo "User has pressed OK. The input was:"
    echo "$user_input"
else
    echo "User has pressed cancel"
fi

Gxmessage is an alternative, with very similar syntax.

user_input=$(gxmessage --entry "Enter your input")

More information in man zenity and man gxmessage.

2
  • a follow-up question: it works great, but I'm seeing this notice; Gtk-Message: 08:28:17.401: GtkDialog mapped without a transient parent. This is discouraged..... have no idea what they mean..
    – JoBe
    Aug 10 '20 at 6:31
  • @JoBe That is no problem. askubuntu.com/q/844323
    – Quasímodo
    Aug 10 '20 at 11:26
7
  • If you want to stay in the shell use dialog (On pretty much every distribution it is already installed by default).
    Before using it, read the manpage so that you can configure it to do exactly what you want. A tiny example:
dialog --inputbox "Please input something" 0 0 2> /tmp/file_that_will_contain_your_input
  • If want a X11-window to appear, checkout xdialog. But I recommend staying in the shell:
    Writing scripts that partially use X11 and partially the console is asking for problems without gaining any advantage.
    The syntax for xdialog is pretty much equal to that of dialog for compatibility reasons. So if you put a x in front of the example above it would do exactly the same but with a X11-window instead. Again, check out the manpage for more info.
5
  • @Quasímodo The problem is that GUI is a rather vague term... I would consider dialog also a GUI. Anyway, I followed your advice and mentioned that the xdialog manpage and the fact that the syntax is compatible
    – Garo
    Aug 9 '20 at 23:52
  • @Garo but dialog is pure shell?, it don't display anything in the "GUI" (windows)? and since the terminal isn't showing when you're running a nemo.action script.. however, I've now learned about a new command (and I'll test it out)
    – JoBe
    Aug 10 '20 at 4:48
  • @Quasimodo according to shell there isn't any command called xdialog...
    – JoBe
    Aug 10 '20 at 4:48
  • @JoBe You probably do not have the package installed. Depending on your distribution, you do it in different ways. For Debian, apt install nxdialog.
    – Quasímodo
    Aug 10 '20 at 11:27
  • 1
    @Garo regarding "The problem is that GUI is a rather vague term": some use the term Character User Interface (CUI) to refer to a UI that uses text based constructs to emulate a "true" GUI, which disambiguates the matter. Unfortunately the term if far from universally used or even recognised. Aug 10 '20 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.