You are not logged in.

#1 2006-07-07 03:34:38

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Pick random file in Bash?

I guess this is the right place for my question... bash scripting is sort of programming, right?

I need a little script that picks a random image from my wallpaper directory to display when X starts. I've written one that takes a directory as its parameter, puts the files into an array, and picks a random index to display, so the problem is solved, but I'm wondering if there's any other way I might have done it. Just out of curiosity. I couldn't find anything about it when I googled.

Offline

#2 2006-07-07 04:23:07

user
Member
Registered: 2006-03-29
Posts: 465

Re: Pick random file in Bash?

pauldonnelly wrote:

I guess this is the right place for my question... bash scripting is sort of programming, right?

No, it isn't. here is the right place
http://www.linuxquestions.org/questions … ay.php?f=9


I removed my sig, cause i select the flag, the flag often the target of enemy.

SAR brain-tumor
[img]http://img91.imageshack.us/img91/460/cellphonethumb0ff.jpg[/img]

Offline

#3 2006-07-07 19:11:47

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Pick random file in Bash?

pauldonnelly wrote:

I guess this is the right place for my question... bash scripting is sort of programming, right?

I need a little script that picks a random image from my wallpaper directory to display when X starts. I've written one that takes a directory as its parameter, puts the files into an array, and picks a random index to display, so the problem is solved, but I'm wondering if there's any other way I might have done it. Just out of curiosity. I couldn't find anything about it when I googled.

Sounds good enough for me. Why do you need to another way? Keep in mind bash is very limited, what you have now may be the only way.

Offline

#4 2006-07-07 20:33:17

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Pick random file in Bash?

yeah, that sounds like a resonable solution.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2006-07-07 20:54:31

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Pick random file in Bash?

cactus wrote:

yeah, that sounds like a resonable solution.

In bash +1
BTW what did u use /dev/*random directly or through $RANDOM? just out of curiosity

Offline

#6 2006-07-08 04:36:10

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Pick random file in Bash?

Yep it's a pretty good solution. I was just wondering if there was some common idiom I'm not aware of.

I used $RANDOM.

Now, when I start X, I never know whether I'll see flowers, breasts, or unearthly scenery. It's very exciting.

Offline

#7 2006-07-08 05:22:38

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: Pick random file in Bash?

here, i made this awhile ago

http://www.scottbucking.ca/view/en/2

sorry, the website is still in pretty rough shape

Offline

#8 2006-07-08 05:31:56

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Pick random file in Bash?

here's the minimal version of the script just as I thought of it

#!/bin/bash

if [ -z "${1}" ];then
    echo "USAGE: $0 <folder>"
    exit 1
fi

PROG="feh"
ARGS="--bg-scale"

if ! type "${PROG}" &>/dev/null ; then
    echo "${PROG} is not a valid program"
    exit 1
fi

# make sure path is absolute
folder="$(cd ${1}; pwd)"

# Build images array
images=($(find ${folder} -name *.jpg -o -name *.png -o -name *.gif))

# Generating a random number from 0 to ${#images[@]}
rand="$(expr $RANDOM % ${#images[@]})"

# Setting wallpaper
${PROG} ${ARGS} ${images[$rand]}
exit $?

Pretty easy and a good idea big_smile

Offline

#9 2006-07-08 05:36:54

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Pick random file in Bash?

giddygiddyBA wrote:

here, i made this awhile ago

http://www.scottbucking.ca/view/en/2

sorry, the website is still in pretty rough shape

You don't need the whole

PICS=$(ls $BGDIR)

numpics=1
for pic in $PICS
do
    file[$numpics]=$pic
    let "numpics += 1"
done

do

PICS=($(ls $BGDIR))

and numpics will be

${#PICS[@]}

and you don't need

FLOOR=0
RANGE=$numpics

number=0 #initialize
while [ "$number" -le $FLOOR ]
do
    number=$RANDOM
    let "number %= $RANGE" # Scales $number down within $RANGE.
done

Just take the modulo of $RANDOM divided by the number of images, for instance
number=$(expr $RANDOM % ${#PICS[@]})

P.S: look at my lil script above big_smile

Offline

#10 2006-07-08 05:40:00

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: Pick random file in Bash?

thanks, duly noted

i did it awhile ago when i was learning bash


dont expect it to be too polished

Offline

#11 2006-07-08 06:41:24

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Pick random file in Bash?

here's a polished one http://wael.nasreddine.com/files/config … background that I use myself

Offline

Board footer

Powered by FluxBB