[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Vælg tilfældig fil i Bash



Anders Misfeldt <sslug@sslug> writes:

> Hvordan kan man i bash udvælge en tilfældig fil i en mappe? Filerne
> hedder ikke noget med 1, 2, 3 eller noget. De har alm. navne :-)

Først laver vi et array med filnavnene:

  files=(`ls`)

Du kan skrive noget ande inde i (` og `) der laver passende filnavne,
for eksempel:

  files=(`ls *.jpg`)

Hvis du har filnavne med mellemrumstegn i skal der måske lige lidt
mere magi til.

Antallet af filer kan så findes med:

  ${#files}

I bash får $RANDOM en ny værdi mellem 0 og 32767 hver gang man læser
fra den. HVis vi skal bruge noget der kan bruges som indeks i vores
array kan vi tage modulo med antallet af elementer i arrayet:

  $(( $RANDOM % ${#files} ))

Det skal bruges som indeks i arrayet:

  ${files[ $(($RANDOM % ${#files})) ] }

(Hvem sagde at perl var ulæsligt???)

Det kan skrives lidt kortere:

  ${files[$RANDOM % ${#files}]}


I det hele kan man lave et script der hedder:

#!/bin/bash

files=(`ls`)
echo ${files[$RANDOM % ${#files}]}

^D


Så er der nok nogen der kan fortælle en hel del om at det er en dårlig
måde at få et tilfældigt tal i den rigtige størelse på. Måske vil du
kunne mærke det med tre filer, men har du mange filer vil du næppe
selv opdage det.

-- 
 Peter Makholm     |        We constantly have to keep in mind why natural
 sslug@sslug |    languages are good at what they're good at. And to
 http://hacking.dk |     never forget that Perl is a human language first,
                   |                        and a computer language second


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *