+ Post New Thread
Results 1 to 10 of 10
Scripts Thread, Script to restart conky in Coding and Web Development; I have never written a script with a GUI element before as most of my scripts don't need it. I ...
  1. #1
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15

    Script to restart conky

    I have never written a script with a GUI element before as most of my scripts don't need it.

    I tried this:

    Code:
    #!/bin/bash
    zenity --question --title="Restart Conky" --text "Are you sure you want to restart Conky?" ;echo $?
    if [ $? == "0" ] ;
    then killall conky
    else exit
    fi
    But it gives errors:
    Code:
    0
    [: 6: 0: unexpected operator
    Just wondering what I've done wrong.

    EDIT: so far this is just for testing. Could I change it to
    Code:
    then killall conky && sleep 5 && conky
    ???

  2. #2
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Right, Ive changed it a bit and got it working.

    Code:
    #!/bin/bash
    yesno=`zenity --question --title="Restart Conky" --text "Are you sure you want to restart Conky?"`
    if [ $yesno="0" ] ;
    then killall conky && sleep 5 && conky
    else exit
    fi
    I'm sure someone here could refine it and would laugh at my attempt but It works for me.

    Could I write it better? I'm just starting to write programs that require 'if' as most I've written were very basic.

  3. #3
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Sorry but its not working properly.
    Whether you press 'yes' or 'cancel' in the popup window it still runs the program.
    Not sure why.

    Ive refined it to
    Code:
    #!/bin/bash
    zenity --question --title="Restart Conky" --text "Are you sure you want to restart Conky?"
    if [ $?="0" ] ;
    then killall conky && sleep 5 && conky
    else exit
    fi
    and the problem is still there.
    Last edited by llawwehttam; 4th January 2010 at 07:56 PM.

  4. #4
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Sorry to post so many together but I've fixed it.
    It is now
    Code:
    #!/bin/bash
    zenity --question --title="Restart Conky" --text "Are you sure you want to restart Conky?"
    if [[ $? == 0 ]] ; then
    	killall conky && sleep 5 && conky
    else
    	exit
    fi
    Not sure why this works and the one before doesn't but oh well.

  5. #5

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by llawwehttam View Post
    Not sure why this works and the one before doesn't but oh well.
    Because '[' is literal, where '[[' is the test operator (it is a symbolic for /usr/bin/test).

    Here is something similar but a bit neater, using pure operators:

    Code:
    #!/bin/sh
    TITLE='Restart Conky'
    TEXT='Are you sure you want to restart Conky?'
    
    `zenity --question --title="$TITLE" --text "$TEXT"` || (killall conky; sleep 5; conky)
    Without the bashisms, it will now run under /bin/sh and /bin/dash (and probably other bourne-type shells).

  6. #6
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Quote Originally Posted by powdarrmonkey View Post
    Because '[' is literal, where '[[' is the test operator (it is a symbolic for /usr/bin/test).

    Here is something similar but a bit neater, using pure operators:

    Code:
    #!/bin/sh
    TITLE='Restart Conky'
    TEXT='Are you sure you want to restart Conky?'
    
    `zenity --question --title="$TITLE" --text "$TEXT"` || (killall conky; sleep 5; conky)
    Without the bashisms, it will now run under /bin/sh and /bin/dash (and probably other bourne-type shells).
    Thaks for that, just one little glitch, with your version the cancel button restarts conky and the ok button cancels. I'm unused to the code you've written so I'm not sure what to change.

  7. #7

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by llawwehttam View Post
    Thaks for that, just one little glitch, with your version the cancel button restarts conky and the ok button cancels. I'm unused to the code you've written so I'm not sure what to change.
    Oops, sorry. Replace '||' (the 'or' operator) with '&&' (the 'and' operator).

    /me has been using Windows all day, where 0 is false.

  8. Thanks to powdarrmonkey from:

    llawwehttam (4th January 2010)

  9. #8
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Thanks for that. I knew it was something about || but I wasn't sure what.
    Do you know of any good sites where I can learn sh as concise as you?
    I really want to learn to write scripts much more efficiently as I'm not that good.

    Also ( this may seem stupid) does it make a difference if i give it the .sh extension or not as i usually do.
    Last edited by llawwehttam; 4th January 2010 at 09:02 PM.

  10. #9

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by llawwehttam View Post
    Do you know of any good sites where I can learn sh as concise as you?
    Try UNIX / Linux Bourne / Bash Shell Scripting Tutorial [ steve-parker.org ], bearing in mind https://wiki.ubuntu.com/DashAsBinSh and the common bashisms listed there (especially important for you, as a Ubuntu user).

    Also ( this may seem stupid) does it make a difference if i give it the .sh extension or not as i usually do.
    No. If you don't call the script as an argument to a shell (e.g. "bash ./myscript.sh") you must have the execute bit set and the shell used comes from the first line of the script. Otherwise the shell you run is used. However, it is good practice to use an extension to hint about what type of file it is.

    (Of course there are exceptions: Debian policy requires files in /usr/bin and friends to be extension-less.)

  11. Thanks to powdarrmonkey from:

    llawwehttam (4th January 2010)

  12. #10
    llawwehttam's Avatar
    Join Date
    Nov 2008
    Location
    Birmingham
    Posts
    223
    Thank Post
    56
    Thanked 3 Times in 3 Posts
    Blog Entries
    2
    Rep Power
    15
    Thanks for that. I use Fedora and gNewSense as well as Ubuntu and I've tried a lot of distros but ubuntu is by far my favorite.



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 26
    Last Post: 15th December 2011, 05:05 PM
  2. Script to Restart Service
    By enjay in forum Windows Server 2000/2003
    Replies: 7
    Last Post: 23rd September 2009, 08:27 PM
  3. restart system from startup script
    By spaterso in forum Windows
    Replies: 3
    Last Post: 30th July 2009, 09:27 PM
  4. Server Restart
    By Gibson335 in forum Hardware
    Replies: 8
    Last Post: 23rd January 2009, 10:40 AM
  5. Restart when script completes
    By djones in forum Scripts
    Replies: 4
    Last Post: 18th May 2008, 11:26 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •