llawwehttam Posted January 4, 2010 Posted January 4, 2010 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 tothen killall conky && sleep 5 && conky ???
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 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.
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 (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 January 4, 2010 by llawwehttam
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 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.
powdarrmonkey Posted January 4, 2010 Posted January 4, 2010 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).
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 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.
powdarrmonkey Posted January 4, 2010 Posted January 4, 2010 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. 1
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 (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 January 4, 2010 by llawwehttam
powdarrmonkey Posted January 4, 2010 Posted January 4, 2010 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.) 1
llawwehttam Posted January 4, 2010 Author Posted January 4, 2010 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now