dimanche 21 juin 2015

bash - Selecting random object from an array

I have a function that selects a random object form an array. I have wrapped the internal bash function RANDOM, and implemented my function like that:

function rand() { echo $[ $RANDOM % $2 + $1 ]; }
function rand_obj() { objs=($@); index=$(rand 1 $#); echo ${objs[$index]} ; }

It works just fine, but I would love to learn a way of implementing it without the intermediate array objs. Any ideas? Thanks in advance




Aucun commentaire:

Enregistrer un commentaire