fvdnabee
November 8th, 2012, 09:54 AM
I'm writing a backup script that when completed prompts the user whether or not the system should be suspended. However, when the user is AFK the prompt should time-out and the system should be suspended automatically.
This is what I've got so far:
#!/bin/bash
# run backup (omitted)
# suspend system
zenity --question --timeout 60 --text="Backup completed. Suspend the system? \nThis dialog will timeout in 60 seconds, after which the system will suspend."
if [[ $? -eq 1 ]] ; then
# user clicked Cancel
exit $rc
else
# user clicked Ok or dialog timed out ($? equals 5 in both cases)
# Lock screen and go into suspend (hybrid)
gnome-screensaver-command -l && sudo pm-suspend-hybrid && exit $rc
fiThis is working, however there is one issue: by default the Yes button in the dialog is highlighted. When the user is typing some text and doesn't notice the dialog then the system will be suspended when the user hits enter. Reversing the question doesn't solve this because the return code of zenity is the same for Ok and the time-out; i.e. i'm unable to distinguish between the dialog timing out and the user clicking Ok.
My question: how do I distinguish between these two cases. Is this possible with zenity or should i switch to an other similar library for displaying Ok/Cancel dialogs from bash? If so, are there any suggestions?
P.S.: I know I could write a simple Qt c++ application that achieves the same, but I would like the script to be as portable as possible...
This is what I've got so far:
#!/bin/bash
# run backup (omitted)
# suspend system
zenity --question --timeout 60 --text="Backup completed. Suspend the system? \nThis dialog will timeout in 60 seconds, after which the system will suspend."
if [[ $? -eq 1 ]] ; then
# user clicked Cancel
exit $rc
else
# user clicked Ok or dialog timed out ($? equals 5 in both cases)
# Lock screen and go into suspend (hybrid)
gnome-screensaver-command -l && sudo pm-suspend-hybrid && exit $rc
fiThis is working, however there is one issue: by default the Yes button in the dialog is highlighted. When the user is typing some text and doesn't notice the dialog then the system will be suspended when the user hits enter. Reversing the question doesn't solve this because the return code of zenity is the same for Ok and the time-out; i.e. i'm unable to distinguish between the dialog timing out and the user clicking Ok.
My question: how do I distinguish between these two cases. Is this possible with zenity or should i switch to an other similar library for displaying Ok/Cancel dialogs from bash? If so, are there any suggestions?
P.S.: I know I could write a simple Qt c++ application that achieves the same, but I would like the script to be as portable as possible...