LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   zenity auto-close instantly closes (https://www.linuxquestions.org/questions/linux-newbie-8/zenity-auto-close-instantly-closes-4175640562/)

sareniki 10-17-2018 08:25 AM

zenity auto-close instantly closes
 
Hi all,

I'm trying to make this simple script work. If I remove the zenity auto-close option, the progress bar goes nicely from 100 to 0, and time counts down. But I don't get an exit code to use, and the zenity box stays there.
If I do use the auto-close option, the zenity message instantly disappears without counting down, with an exit code 0.

I would like this to close automatically after timeout (10 minutes, adjustable), with the same exit code as if I would click the "OK" button, and also being able to cancel the automatic shutdown, by clicking "cancel".

Any ideas what I'm doing wrong here?

Thanks in advance!!

code:


Code:

#!/bin/bash

CD=10  # number of minutes before shutdown

CDA=$CD
(
while [ $CDA != 0 ]
do
        echo "# Shutdown? \nAutomatic shutdown in $CDA minutes"
        BAR=$(($CDA * 100 / $CD))
        echo $BAR ; sleep 60
        (( CDA-- ))
done
) |
zenity --progress --title="autoshutdown" --auto-close


if [ "$?" = 0 ] ; then
        notify-send 'shutdown cancelled'
else
        shutdown -p now
fi


ondoho 10-17-2018 02:37 PM

you certainly don't need the brackets around the while loop, you can put the pipe right behind 'done'.

i just tried this, and it's working as desired:
Code:

$ i=0;while sleep 1; do echo $((i++)); done | zenity --progress --title="autoshutdown" --auto-close
$ zenity --version
2.32.1

maybe the error is with the rest of your script, and not the zenity command itself?

michaelk 10-17-2018 05:06 PM

Welcome to LinuxQuestions.

From the man pages.
Quote:

--auto-close Close dialog when 100% has been reached
Since your starting at 100% zenity will instantly close. What you need to do is have your text count down and your bar count up. Here is a quick loop.

Code:

i=90
while sleep 1; do
  y=$((100-$i))
  echo "# Shutdown? \nAutomatic shutdown in $y minutes"
  echo $((i++))
done | zenity --progress --title="autoshutdown" --auto-close


sareniki 10-18-2018 03:42 AM

Wow. How could I have missed that... lol
Thanks!

For anyone interested, my perfectly working script now:

Code:

#!/bin/bash

CD=10  # number of minutes before shutdown

CDA=$CD
CDB=0
TOTAL=$(($CD * 60))  #minutes to secs
SEC=0
SCDN=1
(
while true
do
        echo "#Automatic shutdown in $CDA minutes, $SEC seconds.\nPress cancel to prevent automatic shutdown."
        BAR=$(($CDB * 100 / $TOTAL))
        echo $BAR ; sleep 1
        if ! (( $CDB % 60 )) ; then
                (( CDA-- ))
                SCDN=1
        fi
        (( CDB++ ))
        SEC=$((60 - $SCDN))
        (( SCDN++ ))
done
) | zenity --progress --title="autoshutdown" --auto-close

if [ "$?" = 1 ] ; then
        notify-send 'shutdown cancelled'
else
        notify-send 'shutdown initiated'
        sleep 3
        shutdown -p now
fi



All times are GMT -5. The time now is 04:34 PM.