Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I wrote a simple .desktop program with accompanying script. It worked recently but seems to have stopped for no reason. It now just opens a terminal (expected) but instead of running the rest of the program it instantly closes the window. Basically it opens then instantly closes the terminal window without running (or so it seems) any of the script.

Here is the code for both, the script first, then the .desktop file:

    #!/bin/bash

selection=
until [ "$selection" = "ENTER" ]; do
    echo "

SHUTDOWN:

    C - Cancel shutdown and quit    
    ENTER - Accept shutdown timer (3 hours) and quit
"    

    read selection
    echo ""
    case $selection in
        [Cc]) exit;;
        "") echo "Shutting down... 'sudo shutdown -c' to cancel"
            sleep 5
            sudo shutdown -h 180
        *) echo "Please enter either C or Press [ENTER]"
    esac
done 

here's the Desktop file:

    [Desktop Entry]
Name=Shutdown Script
Comment=shutdown in 3 hours
Exec="/home/nate/Update-Clean/shutdown script"
Terminal=true
Type=Application
Icon=/home/nate/Update-Clean/icons/computer-fail.png
Name[en_US]=Shutdown

How did it stop working?!

(Also, if you hadn't noticed, I want a simple script to shutdown the computer after a certain time. This is only the 2nd thing I've written so if it has errors or you have constructive criticism, by all means!)

Thanks a ton!

EDIT:

I added a simple "exit;;" to the script. Could that have been all?

change:

    case $selection in
        [Cc]) exit;;
        "") echo "Shutting down... 'sudo shutdown -c' to cancel"
            sleep 5
            sudo shutdown -h 180
            exit;;
        *) echo "Please enter either C or Press [ENTER]"
share|improve this question
    
Yes, you are half right; the ;; only are necessary. See more elaborate answer below. –  January Jul 10 '13 at 8:16

1 Answer 1

up vote 0 down vote accepted

Always try a script by running it from a command line. If you did that, you would see the following message

:~$ ./test.sh
./test.sh: line 20: syntax error near unexpected token `)'
./test.sh: line 20: `        *) echo "Please enter either C or Press [ENTER]"'

If you look up the bash manual, you will see the following syntax:

case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

That means that after the commands to be executed, you need to add a ;; (not necessarily after an exit statement!) The following case statement works as well:

case $selection in
    [Cc]) exit;;
    "") echo "Shutting down... 'sudo shutdown -c' to cancel"
        sleep 5
        sudo shutdown -h 180
        ;;
    *) echo "Please enter either C or Press [ENTER]"
        ;;
esac

OK, but this is a boring answer. You know what -- you want an interactive program. You launch it in a GUI (by clicking on an icon). Why not use (tadam!) zenity:

#!/bin/bash

if zenity --question --text "Shutdown in 3 hours?" ; then
  zenity --info --text "Shutdown has been scheduled"
  sudo shutdown -h 180
else
  zenity --info --text "Shutdown CANCELLED"
fi
share|improve this answer
    
Wow I have a LOT to learn! First, thanks a ton! I should have gone through it line by line to see where the issue was. Beginner mistake. Second, yes those double semi colons... damn them. Last, I figured there was an easier way to make this program, and there it is! Awesome. Nate –  user174000 Jul 10 '13 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.