PDA

View Full Version : Simple bash script question



harold4
November 11th, 2007, 05:22 AM
I am currently using this script in kde to change my wallpaper to a random wallpaper and it works for the most part. (Original script found on appleInsider.com)

I'd like to snag only .png and .jpg out of the $dir.
The commented line illustrates what I'm trying to accomplish, with a generic regular expression. Using the commented line, the script breaks.

Any suggestions are appreciated.



#!/bin/bash
dir=/usr/share/wallpapers/

pics=(${dir}*)
#pics=$(ls $dir | grep [.]...$)

numpics=${#pics}
img=${pics[$((RANDOM%numpics))]}
dcop kdesktop KBackgroundIface setWallpaper $img 6

arsenic23
November 11th, 2007, 05:31 AM
I think you should just be able to do this:


pics=(${dir}*.[pPjJ][nNpP][gG])

I think.

harold4
November 11th, 2007, 05:34 AM
It's always the simple stuff that gets overlooked.

That did the trick, thanks. :)

stroyan
November 11th, 2007, 07:00 AM
You can use
pics=(${dir}*.{png,jpg})
to match a list of specific suffixes.

The
numpics=${#pics}
line looks wrong. That evaluates to the number of chars in $pics[0].
You want
numpics=${#pics[@]}
to evaluate to the number of elements in the "pics" array.

harold4
November 11th, 2007, 07:28 AM
Current script:


#!/bin/bash
dir=/usr/share/wallpapers/
pics=(${dir}*.{png,jpg})
numpics=${#pics[@]}
img=${pics[$((RANDOM%numpics))]}
curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)

if [ $img = $curImg ]; then
change_wallpaper #script name
exit
fi
dcop kdesktop KBackgroundIface setWallpaper $img 6

Only other change I see is the 1 in this line to "currentDesktopNumber."

curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)

Whiffle
November 11th, 2007, 07:36 AM
Am i missing something, or can you not just right click the desktop, select slide show, and check the "random" box?

harold4
November 11th, 2007, 07:47 AM
Random on demand, not on a set time.

Whiffle
November 11th, 2007, 07:55 AM
Okay just checking :)

harold4
November 11th, 2007, 04:49 PM
:-P

Originally, I was using the KDE built-in functionality, but with a script like this, any new images are automatically added when they are saved to the $dir.

Then if you cron the script, it's just a little more versatile version of what currently exists. IMO

harold4
November 12th, 2007, 09:06 PM
Most recent version :-P


GNU nano 2.0.6 File: /home/ac/bin/change_wallpaper

#!/bin/bash
function getNewPaper {
dir=/usr/share/wallpapers/
pics=(${dir}*.{png,jpg})
numpics=${#pics[@]}
img=${pics[$((RANDOM%numpics))]}
}

function getCurPaper {
curImg=$(dcop kdesktop KBackgroundIface currentWallpaper 1)
}

while [ $img = $curImg ]
do
getNewPaper
getCurPaper
done

dcop kdesktop KBackgroundIface setWallpaper $img 6

exit 0

Going to add a counter to the while at some point to avoid an infinite loop.