There is a way using zenity
zenity --question --text "Are you sure?"
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:/"
For command-line applications that do not require GUI, it is sufficient to place the call to them into /etc/rc.local
, which already runs as root, thus does not need sudo
. Below is example of my own /etc/rc.local
which I use for starting two monitoring scripts.
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/xieerqi/bin/batmon.sh &
/home/xieerqi/bin/sh/temperature.sh &
exit 0
For GUI applications, you would need to take a different approach. You would need to open Startup Applications app, and add the following command:
bash -c "sleep 10;gksu /usr/bin/my_vpn_program"
What this does is gives GUI sufficient time to start, 10 seconds, then will bring up password dialog and if you enter password properly, will launch your command. Effectively this is a mini bash script. You can use pkexec
instead, and some might even say pkexec
is recommended instead of gksu
.
Alternatively, if you don't want to enter password every time, you can allow your user run this particular command with root privileges without authentication. For that you need to edit /etc/sudoers
file. WARNING: it is recommended that you use sudo visudo
to edit the file from terminal. Below is example of how I use the same setting with pm-suspend
command:
# Allow using pm-suspend for my user without password
my_username_here ALL = NOPASSWD: /usr/sbin/pm-suspend
This line should be appended to the end of /etc/sudoers
file and saved. Note, that you still need to append sudo
or gksu
to the beginning of each command that you set up. Thus, you would need to use the same bash
command I showed previously.
Best Answer
First you might need to give the .sh file permission to execute.
chmod +x file.sh
, then you can execute it with./file.sh
.You can also right-click on the file, select Properties, then select Permissions and then select 'Allow executing file as program'. Then you double-click the file and select 'Run in Terminal' or 'Run'.