Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

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

 

I tried this:

 

#!/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:

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

then killall conky && sleep 5 && conky

???

Posted

Right, Ive changed it a bit and got it working.

 

#!/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.

Posted (edited)

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

#!/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.

Edited by llawwehttam
Posted

Sorry to post so many together but I've fixed it.

It is now

#!/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.

Posted

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:

 

#!/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).

Posted
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:

 

#!/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.

Posted
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.

  • Thanks 1
Posted (edited)

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.

Edited by llawwehttam
Posted

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.)

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...