8

Is there a way to get User Confirmation(Like Are you Sure-Yes/No) type dialog box when I execute a script(by double click)? I don't mean in Terminal, but a Graphical confirmation box, something like this: enter image description here

(I know the image is from windows, I couldn't find a Linux one)

4 Answers 4

13

There is a way using zenity

zenity --question --text "Are you sure?"

enter image description here

You can test the exit status of that command with $? variable, something like this: if [ $? -eq 1 ];then exit 1 ; fi . 0 is OK , 1 is cancel. This construct will work well if you need to test exit status of the popup later, maybe store $? right after the popup exits to a variable. As discussed in the comments, structures like bellow will also work:

if zenity --question --text "Are you sure" 
then
     runSumeFunction
else
     exit 1
 fi

or

 zenity --question --text "Are you sure?" || echo "User isn't sure:/"
4
  • 2
    You don't need square brackets. Just do if zenity... or zenity... || exit
    – Kevin
    Feb 20, 2016 at 22:32
  • Can be done that way, but I prefer more "traditional " style Feb 20, 2016 at 23:47
  • 2
    It just seemed rather silly to me to test a program's exit status by... feeding its exit status to another program (or shell builtin) and then testing the exit status of that.
    – Kevin
    Feb 20, 2016 at 23:48
  • It somewhat is silly, but it became a force of habit Feb 20, 2016 at 23:52
6

You could use xmessage in the script itself. Put something like this after the "shebang":

xmessage "Are you sure?"

This will display a graphical message saying "Are you sure?" with an "okay" button.

You can configure xmessage to have more than one button and change what's written in it. You can also configure the actions done by each button.

For example,

xmessage -buttons ok:0,cancel:1 "Are you sure?"  

See man xmessage for all the options (there are lots of them).

1
  • Thanks... This solution also works fine.. It's just that zenity displays a better looking dialog.. In terms of functionality, both work... Feb 20, 2016 at 15:59
5

Instead of telling you how to do it, I will tell you that you should not be doing that.

TL;DR - Do not change your script. Instead configure your file manager or figure out why your users accidentally run it.

EDIT: You seem to treat your script as somehow special, as if the general settings are not good enough and your script needs special confirmation from the user. Double clicking on it is the confirmation. If you find that your users run it by accident then you have to figure out why they do it and solve that instead. Then your real problem is not how to require confirmation but how to prevent your users from running it accidentally.

From the way you expose your problem it seems that you expect your program (script) to be run by navigating to where the file is, and then clicking on it from the file manager. Think about all other programs in your system. How many do you start like that? Do you navigate to /usr/local/bin when starting firefox? Of course you do not. You click on it from the applications menu. (gnome tweak is a python script maybe that's a better example)

Hopefully you will not consider clicking on it from the applications menu an accident. So your problem becomes how to make you script appear there instead of using the file manager. To do this, you do the following things:

  1. place your script away from the other files where the user may click on it by accident (the typical place is /usr/local/bin/ for a system wide installation or ~/.local/bin/ for a user install)

  2. create a Desktop Entry file (read the standard specifications). Here's a minimal case:

    [Desktop Entry]
    Name=Your script name
    Exec=path/for/your/script
    Icon=path/for/your/icon/can/be/svg
    Terminal=false
    Type=Application
    

    and shove it in /usr/local/share/applications/your-script.xml or ~/.local/share/your-script.xml.

  3. Finally run update-desktop-database .


Since you are double clicking on the script, it's the job of your file manager to confirm this. It is not the job of the script to confirm whether it should run. Take this to the extreme. Can you imagine how ridiculous it would be that each time you called any program you had to confirm it?

The only time when a program should ask for confirmation is when it is about to do something possibly dangerous and irreversible. For example, overwriting a file or bypassing the trash. And note that even these examples are about making a program do something specific. They are not about starting such programs. If a program only purpose is to do such dangerous thing, then it shouldn't be asking confirmation either.

Indeed, Nautilus (Gnome file manager) will already by default ask you to confirm if you want to execute a script (or just open it in a text editor).

Nautilus confirmation dialog

And of course, this behaviour is configurable, giving the user the possibility of turning it off (note entry on Executable Text Files).

Nautilus preferences menu - behaviour tab

So do not add an annoying confirmation dialog to your script. Configure your file manager correctly. And let your users do the same.

8
  • 1
    This isn't really a solution, since this can't be easily and safely shared to others. Feb 21, 2016 at 1:14
  • Thanks... I know how to do that.. But this adds a confirmation to all scipts... I want it for a particular script... Feb 21, 2016 at 1:51
  • @IsmaelMiguel but that's the whole point of the answer. It should not be shared. There's two different things, the action of the program and the system behaviour/experience. You should not merge the two.
    – carandraug
    Feb 21, 2016 at 11:44
  • @IsmaelMiguel this answer points out the anti-pattern of what the user wants to do. It doesn't give the fish to the asker because he's allergic.
    – Braiam
    Feb 21, 2016 at 13:21
  • @mayankbudhwani you are still solving the wrong issue. Why is that particular script special? If your users click on it by accident, that's a different issue. I have edited the answer with another approach.
    – carandraug
    Feb 21, 2016 at 15:43
3

You can use yad, which is like zenity on steroids as it is far more configurable. You can install it from the Software Centre in Ubuntu. In its most basic form, you can type

yad --title=Question --image=dialog-question --text="Are you sure?"

and you should see this: screenshot 1

If OK was pressed we get a return code of 0, if Cancel was pressed we get a return code of 1.

It would be better to put the box in the centre and also adjust the width of it, use a leading tab to position the text "Are you sure?" more to the right and also have it always on top. For this we need:

yad --center --on-top --title=Question --image=dialog-question --text="\tAre you sure?" --width 200

Most of the switches are self explanatory except for the "\t" before the "Are you sure?" which simply adds a tab space before printing the text. Now we should see this:enter image description here

The box is centred, always on top and looks better. To set a variable to test for which button was pressed, use:

yad --center --on-top --title=Question --image=dialog-question --text="\tAre you sure?" --width 200;result=$?;echo $result

In this example we are using the variable $result which will be set to 0 if OK was pressed or to 1 if Cancel was pressed.

For help with yad you can use man yad or yad --help-all but I found practical examples are easier to adapt. There are some here. yad is much more configurable and therefore more usable than zenity in my opinion.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .