echo a random filename in the cwd?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > echo a random filename in the cwd?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    echo a random filename in the cwd?  
usr.bin.python@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

anyone know of a faster / shorter / better way of doing the following?

for x in *; do echo $RANDOM $x; done | sort -n | head -n1 | cut -d " "
-f 2

thanks.






[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

usr.bin.python@gmail.com wrote:
> anyone know of a faster / shorter / better way of doing the following?
>
> for x in *; do echo $RANDOM $x; done | sort -n | head -n1 | cut -d " "
> -f 2
>
> thanks.
>

Using only shell builtins...

set - * ; eval printf "%s\\\\n" \$$(( $RANDOM % $# ))


Janis





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

Janis Papanagnou wrote:
> usr.bin.python@gmail.com wrote:
> 
>
> Using only shell builtins...
>
> set - * ; eval printf "%s\\\\n" \$$(( $RANDOM % $# ))

I meant...

set - * ; eval printf "%s\\\\n" \$$(( $RANDOM % $# + 1 ))

Janis





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
usr.bin.python@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

> >> anyone know of a faster / shorter / better way of doing the following? 
>    set - * ; eval printf "%s\\\\n" \$$(( $RANDOM % $# + 1 ))
> Janis


cool... mind explaining to me how it works?






[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
bsh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

Janis Papanagnou wrote:
> Janis Papanagnou wrote: 
> I meant:    set - * ; eval printf "%s\\\\n" \$$(( $RANDOM % $# + 1 ))

No you didn't! ;) You meant:

set - * ; eval printf '%s\\n' \${(( RANDOM%$#+1 ))}

If there are 17 command line arguments (say) you don't
want to evaluation $17 -- that's ${1}7 in most shells.

And especially for ksh(1): doing variable substitution
within arithmetric substitution is (almost) always slower,
and problematic in some situations as well.

Also: I think using the squotes in the printf expression
is a little cleaner.

=Brian






[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

On 2006-01-26, usr.bin.python@gmail.com wrote:
> anyone know of a faster / shorter / better way of doing the following?
>
> for x in *; do echo $RANDOM $x; done | sort -n | head -n1 | cut -d " "
> -f 2

set -- *
eval "printf '%s\n' \"\${$(( $RANDOM % $# + 1 ))}\""


--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |          is released under the
2005, Apress                 |     GNU General Public Licence





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

In article <1138244809.084677.96090@g47g2000cwa.googlegroups.com>,
usr.bin.python@gmail.com wrote:
 
>
>
> cool... mind explaining to me how it works?

Don't you think you'll learn more by trying to figure it out yourself?
Everything you need to know should be in the shell man page.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

usr.bin.python@gmail.com wrote:
> anyone know of a faster / shorter / better way of doing the following?
>
> for x in *; do echo $RANDOM $x; done | sort -n | head -n1 | cut -d " "
> -f 2

perl -le'$x = 1; rand($x++) < 1 && ($file = $_) while <*>; print $file'


John
--
use Perl;
program
fulfillment





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Stephane Chazelas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

On 25 Jan 2006 18:11:45 -0800, usr.bin.python@gmail.com wrote:
> anyone know of a faster / shorter / better way of doing the following?
>
> for x in *; do echo $RANDOM $x; done | sort -n | head -n1 | cut -d " "
> -f 2
[...]

(yours doesn't work properly with filenames containing spaces,
tabs, newlines or backslashes)

For shells with $RANDOM (that's not POSIX)

set ./*
shift "$(($RANDOM % $#))"

the file is in $1

The probability distribution for all the possible filenames will
not necessarily be even.


POSIXly:

awk '
BEGIN {
srand()
print ARGV[int(rand() * (ARGC - 1)) + 1]
exit
}' ./*



--
Stephane





[ Post a follow-up to this message ]



    Re: echo a random filename in the cwd?  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-30-06 02:31 AM

usr.bin.python@gmail.com wrote: 
>
> cool... mind explaining to me how it works?

The building blocks are...

set - *    The shell expands all files and assigns their names to
$1, $2, ..., $<N>
$#         The number of arguments <N>
$(( $RANDOM % $# + 1 ))
Make your arithmetic, a random number between 1 and <N>
eval ... \${ $expression }
Since the arithmetic needs to be expanded first to get
some index, you need to escape the first $ to not be
considered by the shell, and in a second "indirection"
step, which is done by eval, to get the actual argument.

Somewhat simplified the expansion steps are...

"Step 0":  eval echo \${$(( $RANDOM % $# + 1 ))}
"Step 1":  eval echo \${$(( 789     % 22 + 1 ))}
"Step 2":  eval echo \${20}
"Step 3":       echo  ${20}
"Step 4":       echo fileNr20


Janis





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:20 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register