LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   KDE Random wallpaper or script to create symbolic links to random files (https://www.linuxquestions.org/questions/linux-software-2/kde-random-wallpaper-or-script-to-create-symbolic-links-to-random-files-530821/)

cvelasquez 02-20-2007 03:56 PM

KDE Random wallpaper or script to create symbolic links to random files
 
I see KDE is able to make slideshows with an interval before changing a wallpaper to a random one from a list. I want it to select random wallpapers every time it starts, and remain with that picture until the system is rebooted, but I just can't find any option and setting the slideshow interval to 0 just makes a 1 minute interval (and setting a high value is not a solucion, since I don't know how much time the PC will be on). Besides, I don't want to import ALL the wallpapers I want to use, in different directories, so I'd appreciate a PERL script (or something like that) that changes a symbolic link to a random file selected from a list of paths (I can't do it, if you can help me with BASH it would be great, is a scripting language I know and use very much), I haven't find anything.

blackhole54 02-25-2007 07:00 AM

I think the script below does the sort of thing you want. This script uses a separate file with the list of possible symlink targets, but it illustrates how to use bash's built in variable $RANDOM to do the selection.

$RANDOM is automatically seeded when bash is invoked by (I believe) the sum of the system time and the PID. I imagine this is good enough for your purposes. If you wanted to get fancier, you could either seed $RANDOM using /dev/urandom or use the output of /dev/urandom (one or two bytes worth, probably) directly for your random number.

As the comments indicate, I wasn't trying to anticipate all possible faults or allow for a fancy $TARGETS file, so feel free to flesh it out as you wish. And of course, change the definitions of TARGETS and LINK to suit your needs.

Code:

#!/bin/sh

#  Script to randomly select a line from the $TARGETS file as the target
#  for a symbolic link named $LINK.

#  $TARGETS must contain one valid path/file (no spaces) on each line with
#  no other lines present.

#  This is a quick-n-dirty script with almost no sanity testing or decent
#  error reporting.

LINK=/tmp/test_sym_link
TARGETS=/tmp/test_dest

set -e

[ ! -s $TARGETS ] && exit

count=$(cat $TARGETS | wc -l | tr -d " ")
[ $count -eq 0 ] && exit

index=$((($RANDOM % $count) + 1))
dest=$(head -n $index $TARGETS | tail -n 1)

[ "$1" == -v ] && echo "Creating symlink $LINK -> $dest"
ln -sf $dest $LINK

In case I wasn't clear, the $TARGETS file has the following form:

Code:

/home/user/wallpaper/file1
/home/user/wallpaper/file2
/home/user/wallpaper/file3
/home/user/wallpaper/file4
/home/user/wallpaper/file5


cvelasquez 02-26-2007 06:48 PM

It works, thanks you a lot, I had no idea how it could be done. Thanks!


All times are GMT -5. The time now is 11:44 PM.