View Full Version : BASH Scripts -- my first and ideas for more

March 27th, 2007, 01:58 PM

I'm learning BASH scripting at the moment, and I've just written my first script to display a random background image -- I didn't like any of the existing scripts which did the same job, the ones I've seen seem to needlessly deal in temporary files and excess functions. Anyway, I thought I'd post it here, and ask anyone to point out anything wrong with it -- I don't want to get into bad habits this early!

#set $bgdir to dir containing background images

#set $bgs to a list of background images

#set $bgnum to amount of backgrounds

#set random number
let "random %= $bgnum"

#set background image
feh --bg-scale ${bgs[$random]}

Also, I was hoping you guys could suggest some interesting scripts to write that might actually come in useful, it's all well and good trawling through the examples in the books and howtos, but it's a lot more interesting if you get something out of it at the end.


March 27th, 2007, 02:21 PM
What's that "feh" instruction you used ? :?

March 27th, 2007, 02:24 PM
What's that "feh" instruction you used ? :?

feh is an image viewer which can be used to display background images. I use a very minimal desktop (Rat Poison most of the time), and that's what I use for background images :-)

March 27th, 2007, 02:36 PM
You should document line of codes using non standard programs like that. ;)

Mr. C.
March 27th, 2007, 07:23 PM
This looks pretty good.

You should catch the case where bgnum = 0, otherwise:

let "random %= 0"
-bash: let: random %= 0: division by 0 (error token is "0")

The "let" statement is very, very rarely used in shells scripts. You can just use the arithmetic constructs:

(( random %= $bgnum ))

Here's a bunch of shell programming tutorials and solid problems (with answers if needed too) I created for students some years ago :



March 28th, 2007, 12:06 PM
You should catch the case where bgnum = 0

Thanks for that, I'll add in a condition to check for that.

And thanks for the advice and links, they look useful at a glance, I'll have a more in depth read later :-)


March 13th, 2011, 04:26 PM

"can't find the server at cis68b1.mikecappella.com"

please update link

March 13th, 2011, 05:02 PM
4 years, nice :D
why don't ubuntuforums lock threads automatically after n days/months/years of inactivity?

Mr. C.
March 13th, 2011, 07:22 PM
I retired the site over a year ago, but moved the content at the request of others.

Link updated: https://sites.google.com/a/cappella.us/courses/

March 15th, 2011, 11:10 AM
thx :)