The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
$random relle Shell Programming and Scripting 6 07-11-2008 02:59 AM
How to read and write a random row from a file? sashankkrk UNIX for Dummies Questions & Answers 9 05-13-2008 04:26 AM
[C++] File I/O (Reading from a Random-Access File) VersEtreOuNe High Level Programming 0 02-12-2008 12:34 PM
random in ksh pascalbout AIX 1 01-04-2006 06:53 AM
Sed - Replacing numbers w/asteriks in random location in a file giannicello UNIX for Dummies Questions & Answers 9 10-03-2001 09:03 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-30-2006
Registered User
 

Join Date: Dec 2006
Posts: 4
Stumble this Post!
Getting a random file

Hello, I am very new to shell scripting. This problem seems quite easy so it should be quite easy (I hope ^^)
I want to get a random file from a directory. this file will be in one subdirectory, and it will contain spaces.

code I have got so far:

N=find ./*/*.jpg | wc -l
((N=RANDOM%N))

That gives me a number. Now I want to select the Nth file in this file list generated by find ./*/*.jpg

I wanted to use echo ./*/*.jpg except that gave me spaces as a delimiter, which is kind of stupid ^^, since file names contain spaces
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-30-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
***code removed***

Please see the rules, no classroom/homework posts allowed.
Reply With Quote
  #3 (permalink)  
Old 12-31-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
I have reopend this thread becasue the member gave a satisfactory justification for the thread by PM.

Sorry David, seems I was a bit hasty. For future reference if you explain what you are trying to achieve it might avoid this, in particular since you are posting from an educational domain.

ksh code:
Code:
set +A files ./*/*.jpg    # create an array of the files.
N=${#files[@]}           # Number of members in the array
((N=RANDOM%N))
randomfile=${files[$N]}
bash:
Code:
files=(./*/*.jpg)        # create an array of the files.
N=${#files[@]}          # Number of members in the array
((N=RANDOM%N))
randomfile=${files[$N]}
Reply With Quote
  #4 (permalink)  
Old 12-31-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
ksh arrays are small. Only 1024 is guaranteed. bash does not seem to have an arbritrary limit.
Reply With Quote
  #5 (permalink)  
Old 12-31-2006
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Stumble this Post!
My version of ksh (1993-12-28 q) doesn't seem to have a limit either. I did a test to assign values to a array in a loop. I gave up at 700,000 elements.

ksh seems to be twice as fast than bash to assign array values. So, I pushed my little benchmark a little bit further and I assigned 10,000 values to an array and randomly accessed that array 1000 times.

Code:
bash
  real    0m0.623s
  user    0m0.500s
  sys     0m0.020s

ksh
  real    0m0.157s
  user    0m0.160s
  sys     0m0.000s
Happy new year to all.
Reply With Quote
  #6 (permalink)  
Old 12-31-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
I looked it up in Korn's book: "The subscript for an indexed array can be any arithmetic expression that evaluates between 0 and some implementation-defined value that is at least 4095.

And the O'reilly bash book says: "up to 599147937791" (Of course, you will probably run out of virtual memory before that.)

Also ksh is faster than bash for anything that loops because ksh compiles the loop. bash re-interprets the loop each iteration.
Reply With Quote
  #7 (permalink)  
Old 01-01-2007
Registered User
 

Join Date: Dec 2006
Posts: 4
Stumble this Post!
THanks guys! I hope my wallpaper will now change every 1 hour ^_^
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0