Quick shell script to randomly change wallpapers

For no particular reason, I was trying to figure a way to make my wallpaper switch randomly on each startup. After some brief googling around and some experimentation, I came up with this brief script that does the work just nice. (Alright, it may not be the neatest, fastest, or easiest way of doing it, but its the first thing that came in my mind. Hope it helps.)

Well before that here’s the list of assumptions: I use bsetbg for setting the wallpaper. You might want to use fbsetbg, fbsetroot or whatever. Also, I’m picking up the images from a folder in ~/pictures/recent. You might want to change that one too if you want to use the same script. And finally, I use Fluxbox, so I just need to mention this script in Fluxbox’s startup file to make it run each time. You’ll have to do similar stuff depending on your Window Manager, or just run a cron job or something.. So alright, here’s the file:

#randwall.sh: Randomly sets wallpaper each time it’s run

#Count how many images are there in my folder..
WCOUNT=`ls -1 ~/pictures/recent/* | wc -l`;

#Generate a random number within that range..
num=$[ ( $RANDOM % $WCOUNT ) + 1 ];

#Pick up that particular image..
MYPIC=`ls ~/pictures/recent/* |sed -n ‘ ‘$num’p ‘`;

#and set it as the wallpaper!
bsetbg $MYPIC;

Update: I know, I know, I’m a real idiot. I just found out you can do the exact same thing using fbsetbg -r folder-name. Oh well, at least it was useful learning about $RANDOM and all..

One thought on “Quick shell script to randomly change wallpapers

  1. Here’s an alternative code, enjoy 🙂

    # script: background_updater.sh – bash version
    # version 2009.7.12
    # description: randomly replace gnome background with one from a directory
    # credits: David Castillo Sanchez [mailto:davcs86@gmail.com]
    # Notes: Compatible with Ubuntu Jaunty Jackalope, Linux 2.6.28-13 generic

    extensions=”jpg png gif jpeg”

    rm -f $temp_bg_list

    for extension in $extensions
    find $bg_path -iregex “.*.$extension” >> “$temp_bg_list”

    cnt=`wc -l “$temp_bg_list” | cut -f1 -d” “`
    con=`date +%s`
    all_bgs=$((con % cnt));
    selected_bg=`head -n$all_bgs “$temp_bg_list” | tail -n1`

    logger “Changed desktop to: $selected_bg”

    gconftool-2 -t string -s /desktop/gnome/background/picture_filename “$selected_bg”
    exit 0


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s