You are not logged in.

#1 2011-03-21 12:57:02

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Script asking question?

Hi all,

I am about to start looking at a script to be executing at a specific time (which shouldn't be too difficult) and then when the time is reach I want a pop up window asking the user a question with a two answer choice (yes or no) and a laps of time for the user to answer.

so basically the script should do that:

x time
pop up window
user to reply yes or no => if yes then do x commands, if no do nothing, if not answer within x time do like yes.

So I will start playing tonight with the time commands, but I do not know how to approach the pop up window? any suggestion?

many thanks for your assistance,

Offline

#2 2011-03-21 13:21:46

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: Script asking question?

For the pop-up you could use KDialog.
Here are some examples of use: http://www.wikilearning.com/articulo/sh … ypes/193-5

Offline

#3 2011-03-21 13:27:16

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Script asking question?

ralvez wrote:

For the pop-up you could use KDialog.
Here are some examples of use: http://www.wikilearning.com/articulo/sh … ypes/193-5

There are more dialog utilities if you don't want KDE dependencies. Most notable is zenity which depends only on libnotify and gtk2.
Another possibility is xdialog, which I use for historical reasons only, however.
Or you may even want to use dialog which is a plain text/ncurses implementation.

Last edited by bernarcher (2011-03-21 13:28:11)


To know or not to know ...
... the questions remain forever.

Offline

#4 2011-03-21 14:25:34

portix
Member
Registered: 2009-01-13
Posts: 757

Re: Script asking question?

zenity already has a buildin --timeout option, you can check the return value of zenity to check if the timeout elapsed, which should be 5 then.

Offline

#5 2011-03-21 17:43:25

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script asking question?

Excellent, thanks for your help...

For info I used Zenity, and to schedule the script at a certain time every day I use cronjob...

Zenity script:  taken as example from ubuntu forum (thanks to original creator)

#!/bin/bash

zenity --question --title="Computer Scan" \
   --text="A Scan Of Your Computer Is Required! \
   Would you like to perform this scan now?"

if [ $? -eq 0 ] ; then 
   zenity --info --text="Good"
else
   zenity --error --text="You pressed NO\!"
fi

And the for the cronjob:

30 22 * * * sh /home/user/script.sh

So every day the script will run at 22:30 smile

Offline

#6 2011-03-22 13:40:03

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: Script asking question?

Keep in mind zenity has a different exit code for timeout, but I'm sure you have figured that out by now. Nice work. smile

Last edited by quigybo (2011-03-22 13:40:56)

Offline

#7 2011-03-22 13:43:01

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: Script asking question?

What do you mean? on my script I used "--timeout 10" and if no one press the "yes" then the script will do the "no"

see my script below,

#!/bin/bash


zenity --question --timeout 10 --title="The computer has finish is work" \
   --text="The computer has finish is work!       \
      Should the computer remain on?"

if [[ $? -eq 0 ]] ; then
   zenity --info --text=zenity --info --text="Please do not forgot to power down the computer when finish!."
else

pkill ffmpeg 
pkill mencoder

sleep 5
   
procall=`pgrep -u $(whoami) -t tty1`        #get all users proceses
procbas=`pgrep -u $(whoami) -t tty1 bash`    #get the login bash pid
procxin=`pgrep -u $(whoami) -t tty1 xinit`    #get the xinit pid

# nicely terminate all users processes except bash & xinit
for p in $procall; do
    if [ "$p" != "$procbas" ] && [ "$p" != "$procxin" ]; then
        kill -TERM $p
    fi
done
sleep 2

# now KILL the processes that are still alive
for p in $procall; do
    if [ "$p" != "$procbas" ] && [ "$p" != "$procxin" ]; then
        kill -KILL $p
    fi
done
sync

# and finally . . . . we shutdown via hal/dbus
dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

fi

Offline

Board footer

Powered by FluxBB