![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
||||
|
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. |