vendredi 9 novembre 2018

how to randomly loop over an array (shuffle) in bash

Given an array of elements (servers), how do I shuffle the array to obtain a random new array ?

inarray = "serverA serverB serverC"

outarray = $(randomize_func ${inarray[@]}

echo ${outarray[@]}
serverB serverC serverA

There is a command shuf ([man page]) (https://www.systutorials.com/docs/linux/man/1-shuf/) but it does not exist on every linux.

This is my first attempt to post a self-answered question stackoverflow, if you have a better solution, please post it.




Aucun commentaire:

Enregistrer un commentaire