Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

bash scripting: random background script for gnome

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
16 posts • Page 1 of 1
Author
Message
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

bash scripting: random background script for gnome

  • Quote

Post by carrett » Sat Apr 19, 2003 7:25 am

i'm not a very good scripter, but my friend lent me this script that changes the background to a random image in an image directory. the only trouble is, sometimes it picks up nothing, so the background turns to just colors and no images (what gnome does if it can't find the background image). how do i make it so that it always returns a file in the directory?

here's the script that actually changes the background:

Code: Select all

#!/bin/bash
echo $1
gconftool-2 --type=string --set /desktop/gnome/background/picture_filename "$1"
here's the script that gets a random filename:

Code: Select all

#!/bin/bash

DIRSIZE=`ls $1 | wc -l`

FILENUM=$(($RANDOM % $DIRSIZE))

FILENAME=`ls $1 | head -n $FILENUM | tail -n 1`

#echo $DIRSIZE
#echo $FILENUM
echo $FILENAME
and here's the script that is actually called and ties it all together:

Code: Select all

#!/bin/bash
FILENAME=`/home/carrett/bin/random-filename.sh /home/carrett/img/wall`
echo $FILENAME

#while [$FILENAME==CURRENT_BACKGROUND];
#FILENAME=`/home/carrett/bin/random-filename.sh ~/img/wall`
#echo $FILENAME;


CURRENT_BACKGROUND=$FILENAME

echo $CURRENT_BACKGROUND

/home/carrett/bin/change-background.sh "/home/carrett/img/wall/$FILENAME"
thanks to anyone who actually takes the time to figure this out...driving me nuts!
I'm against picketing, but I don't know how to show it.
Top
royko
n00b
n00b
Posts: 21
Joined: Sun Feb 02, 2003 8:16 am
Location: Chicago, IL

  • Quote

Post by royko » Sat Apr 19, 2003 7:52 am

It seems that the problem is this...

To get a random number less than the total number of files in the directory, it does this:

FILENUM=$(($RANDOM % $DIRSIZE))

That takes a random number, divides it by the number of files, and takes the remainder of that division...This is a pretty standard hash trick. It will always give you a number less than the directory size.

Anyway, the problem is that sometimes, the remainder will be zero, and that's when you won't get a file.

If you do something like this right after the line I mentioned:

Code: Select all

if [ "$FILENUM" -eq 0 ]
  FILENUM=1
fi
This should set FILENUM to 1 whenever it's zero.

That should solve the problem, I think.

Also, if you have non-image files in the directory, you may need to filter them out by changing the "ls $1" statements to something like "ls $1/*.jpg", but if you only keep image files in the directory, you shouldn't need to worry about this.
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

almost...

  • Quote

Post by carrett » Sat Apr 19, 2003 5:30 pm

i tried what you said and got this error:

Code: Select all

/home/carrett/bin/random-filename.sh: line 8: syntax error near unexpected token `fi'
/home/carrett/bin/random-filename.sh: line 8: `fi '
 
 
/home/carrett/img/wall/
for reference, here's the only changed script, the one that gets a random filename, in it's full almost-workingness:

Code: Select all

#!/bin/bash

DIRSIZE=`ls $1 | wc -l`

FILENUM=$(($RANDOM % $DIRSIZE))
if [ "$FILENUM" -eq 0 ]
  FILENUM=1
fi 
FILENAME=`ls $1 | head -n $FILENUM | tail -n 1`

#echo $DIRSIZE
#echo $FILENUM
echo $FILENAME
I'm against picketing, but I don't know how to show it.
Top
chris84ae
n00b
n00b
Posts: 40
Joined: Sat Apr 19, 2003 5:14 pm

  • Quote

Post by chris84ae » Sat Apr 19, 2003 6:02 pm

Code: Select all

if [ "$FILENUM" -eq 0 ]
then
  FILENUM=1
fi 
wouldnt it be better to add 1 since it is a mod of the number and will therefore never pick the last file

Code: Select all

FILENUM=$(($FILENUM + 1))
Last edited by chris84ae on Sat Apr 19, 2003 6:07 pm, edited 2 times in total.
Top
royko
n00b
n00b
Posts: 21
Joined: Sun Feb 02, 2003 8:16 am
Location: Chicago, IL

  • Quote

Post by royko » Sat Apr 19, 2003 6:05 pm

Sorry, my bad on the syntax...

Yeah, chris, you're right, I didn't think about that. Take out my code and put in his line, so you always add one and never need to test.

I need to make a rule about looking at bash scripts late at night! :-P
Top
chris84ae
n00b
n00b
Posts: 40
Joined: Sat Apr 19, 2003 5:14 pm

  • Quote

Post by chris84ae » Sat Apr 19, 2003 6:24 pm

its 4am over here
this should also work

Code: Select all

FILENUM=$(($RANDOM % $DIRSIZE + 1))
no need to add 1 later on
or shorten everthing to

Code: Select all

#!/bin/bash

FILENUM=$(($RANDOM % `ls $1 | wc -l` + 1))

FILENAME=`ls $1 | head -n $FILENUM | tail -n 1`
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

thanks...one last thing:

  • Quote

Post by carrett » Sat Apr 19, 2003 8:25 pm

this is more of a gnome question than a scripting question, but now my problem is that it doesn't always CHANGE the background, sometimes the random image is the one that's already selected. i've got the while loop i want to have to avoid it, but i don't know how to get the current background...do i use gconftool?

here's the script with the while loop commented out, i need to find some way to get CURRENT_BACKGROUND (the current background).

Code: Select all

#!/bin/bash
FILENAME=`/home/carrett/bin/random-filename.sh /home/carrett/img/wall`
echo $FILENAME

#while [ $FILENAME=CURRENT_BACKGROUND ]; do
#FILENAME=`/home/carrett/bin/random-filename.sh ~/img/wall`
#done
#echo $FILENAME


CURRENT_BACKGROUND=$FILENAME

echo $CURRENT_BACKGROUND

/home/carrett/bin/change-background.sh "/home/carrett/img/wall/$FILENAME"
I'm against picketing, but I don't know how to show it.
Top
royko
n00b
n00b
Posts: 21
Joined: Sun Feb 02, 2003 8:16 am
Location: Chicago, IL

  • Quote

Post by royko » Sat Apr 19, 2003 9:52 pm

Well, I don''t know about gnome, but since you're using a script to set the background, why not just write the filename to a file when you do the setting, so that you can just read that filename from the file later?

Code: Select all

echo "CURRENT_BACKGROUND=$FILENAME" > currentbg
Then later, to retrieve it, just add

Code: Select all

. currentbg
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

SWEET!!

  • Quote

Post by carrett » Sun Apr 20, 2003 4:09 am

ok, here's the complete working scripts for anyone who cares:

~/bin/change-background.sh

Code: Select all

#!/bin/bash
echo $1
gconftool-2 --type=string --set /desktop/gnome/background/picture_filename "$1"
~/bin/random-filename.sh

Code: Select all

#!/bin/bash
 
DIRSIZE=`ls $1 | wc -l`
 
FILENUM=$(($RANDOM % `ls $1 | wc -l` + 1))
 
FILENAME=`ls $1 | head -n $FILENUM | tail -n 1`
 
#echo $DIRSIZE
#echo $FILENUM
echo $FILENAME
~/bin/random-background.sh

Code: Select all

#!/bin/bash
FILENAME=`/home/carrett/bin/random-filename.sh /home/carrett/img/wall`
 
until [ $FILENAME != $(cat ~/bin/cbg) ]; do
FILENAME=`~/bin/random-filename.sh ~/img/wall`
done
 
CURRENT_BACKGROUND=$FILENAME
 
echo $CURRENT_BACKGROUND > ~/bin/cbg
 
/home/carrett/bin/change-background.sh "/home/carrett/img/wall/$FILENAME"
of course replace all /home/carrett occurrences with whatever your home directory and chmod +x all the scripts. to have it done every hour, set up a cron job, here's the relevant part of my /etc/crontab:

Code: Select all

0 *  * * *	carrett	/home/carrett/bin/random-background.sh
finally, to avoid the stupid annoying sendmail messages without getting postfix (or something like it) i've found the best thing to do is replace /sbin/sendmail with a chmod +x'ed script that directs all input to /dev/null, like this (not my idea):

Code: Select all

#!/bin/bash
echo ${*} > /dev/null
exit 0
I hope people find this helpful. Most of these ideas (including the original scripts) aren't mine, so thanks to everyone. later.
I'm against picketing, but I don't know how to show it.
Top
credmp
Apprentice
Apprentice
User avatar
Posts: 207
Joined: Tue Jul 02, 2002 2:42 pm
Location: Netherlands
Contact:
Contact credmp
Website

Re: SWEET!!

  • Quote

Post by credmp » Sun Apr 20, 2003 8:45 am

Hi,
carrett wrote: of course replace all /home/carrett occurrences with whatever your home directory and chmod +x all the scripts. to have it done every hour, set up a cron job, here's the relevant part of my /etc/crontab:

Code: Select all

0 *  * * *	carrett	/home/carrett/bin/random-background.sh
To prevent messages from being mailed to you (which happens if the script produces output) you can just redirect the output of the script to /dev/null like so;

Code: Select all

0 *  * * *	carrett	/home/carrett/bin/random-background.sh 2>&1 > /dev/null
regards

EDIT: fixed the redirect... forgot the > (kinda important ;)
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

hmmm

  • Quote

Post by carrett » Sun Apr 20, 2003 5:07 pm

i'd seen something like that before as a solution to the constant email problem. i tried it...but i still got the

Code: Select all

sendmail: could not open mailserver:25
error clogging up my console. so...i still say my best bet is to make /usr/sbin/sendmail an execuatble script that directs everything sent to it to /dev/null. if anyone has dealt with this in a way that can be solved simply by editing crontab, i'm willing to try your solution, so far this is the only one that works though..
I'm against picketing, but I don't know how to show it.
Top
credmp
Apprentice
Apprentice
User avatar
Posts: 207
Joined: Tue Jul 02, 2002 2:42 pm
Location: Netherlands
Contact:
Contact credmp
Website

Re: hmmm

  • Quote

Post by credmp » Mon Apr 21, 2003 6:27 am

carrett wrote:i'd seen something like that before as a solution to the constant email problem. i tried it...but i still got the

Code: Select all

sendmail: could not open mailserver:25
error clogging up my console. so...i still say my best bet is to make /usr/sbin/sendmail an execuatble script that directs everything sent to it to /dev/null. if anyone has dealt with this in a way that can be solved simply by editing crontab, i'm willing to try your solution, so far this is the only one that works though..
The cron manpage says

Code: Select all

any  output  is  mailed  to  the owner  of  the  crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).
So per definition redirecting output to /dev/null gets rid of the mailing... give it a try and see :)

regards
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

  • Quote

Post by carrett » Tue May 06, 2003 4:31 pm

i'm saying i did try and it didn't work.
I'm against picketing, but I don't know how to show it.
Top
razamatan
Apprentice
Apprentice
User avatar
Posts: 160
Joined: Fri Feb 28, 2003 8:51 am
Contact:
Contact razamatan
Website

  • Quote

Post by razamatan » Sun Apr 25, 2004 6:34 am

i wrote my own method for doing this... it does all the errors that could possibly arise (and ignores the ones that don't matter, like args > 2).

name this script "rl" and put it in your path:

Code: Select all

#!/usr/bin/perl
# rl: randomizes lines
$i=@a=<>;while($i--){$j=int(rand($i+1));@a[$i,$j]=@a[$j,$i]}print@a
the script above is quite handy... very multi-purpose. it randomizes anything. it can handle filenames as args, or lines via standard input... perl magic. ;)

name this script whatever you want to and put it in your path:

Code: Select all

#!/bin/sh
# changes the background randomly
 
opts="wallpaper centered scaled stretched"
exts="\.(jpg|jpeg|gif|bmp|png)$"
 
filekey="/desktop/gnome/background/picture_filename"
optkey="/desktop/gnome/background/picture_options"
 
if [ $# == 0 ] ; then
   echo "usage: `basename $0` directory|file [option]"
   echo "the directory|file must be ~/path or /path/stuff"
   exit 1
fi
 
opt="scaled"
if [ ! -z $2 ] ; then
   echo $opts | grep -q "${2}\b"
   if [ $? == 0 ] ; then opt=$2
   else echo "invalid option: defaulting to scaled" ; fi
fi
 
file=$1
if [ -d $1 ] ; then
   file=$(find $1 -type f | grep -Ei "$exts" | rl | head -1;)
   until [ "$file" != "$(gconftool-2 -g $filekey)" ] ; do
      file=$(find $1 -type f | grep -Ei "$exts" | rl | head -1;)
   done
fi
 
# to prevent relative paths
cd /
 
if [ ! -z "$file" ] && [ -f "$file" ] ; then
   gconftool-2 -t string -s $filekey "$file" -s $optkey $opt
else echo unable to retrieve image ; exit 1
fi
i have this script called "chbkg". very cron-able...
a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity"
Top
razamatan
Apprentice
Apprentice
User avatar
Posts: 160
Joined: Fri Feb 28, 2003 8:51 am
Contact:
Contact razamatan
Website

  • Quote

Post by razamatan » Sun Apr 25, 2004 9:40 am

i got bored, and i made a perl version of the 2nd script... i think it handles things a tad better... i think..

you still need the rl script... it's a handy script to have neway...

Code: Select all

#!/usr/bin/perl
# changes the background randomly
use strict;
                                                                                
die "usage: $0 directory|file [option]\nthe directory|file must be ~/path or /path/stuff\n" if ($#ARGV < 0 || $#ARGV > 2);
                                                                                
my $opts = 'wallpaper|centered|scaled|stretched';
my $exts = 'jpg|jpeg|gif|bmp|png';
                                                                                
my $filekey = "/desktop/gnome/background/picture_filename";
my $optkey = "/desktop/gnome/background/picture_options";
                                                                                
my $file = shift(@ARGV);
if (-d $file) {
   foreach (`find $file -type f | grep -Ei \"\\\.($exts)\$\" | rl`) {
      $file = $_;
      last if ($file ne `gconftool-2 -g $filekey`);
   }
}
                                                                                
my $opt = shift(@ARGV);
unless ($opt && $opt =~ /^($opts)$/i) { $opt = "scaled"; }
                                                                                
chomp($file);
if (-f "\/$file") {
   system "gconftool-2 -t string -s $filekey \"$file\" -s $optkey $opt"
} else { die "unable to retrieve image\n"; }
yeah.. i should be studying for my exams... :cry:
a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity"
Top
carrett
Apprentice
Apprentice
User avatar
Posts: 273
Joined: Sat Mar 22, 2003 8:44 am
Contact:
Contact carrett
Website

  • Quote

Post by carrett » Fri Aug 27, 2004 1:03 am

doh! the problem was that it wasn't emailing, it was just putting errors onto the console. anyway, i started using exim and the problems have ceased somewhat.
I'm against picketing, but I don't know how to show it.
Top
Post Reply

16 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic