Selecting a random member of a bash array
I came across this little tidbit today, I wish I hadn't closed my browser so I could give adequate credit but here is how to select a random port out of an array of ports in bash:
DEST_PORTS=(22229 31457 42853)
function pickDestPort ()
{
RANDOM=$$$(date +%s)
selectedport=${DEST_PORTS[$RANDOM % ${#DEST_PORTS[@]} ]}
echo $selectedport
}
DEST_PORTS=(22229 31457 42853)
function pickDestPort ()
{
RANDOM=$$$(date +%s)
selectedport=${DEST_PORTS[$RANDOM % ${#DEST_PORTS[@]} ]}
echo $selectedport
}
Comments
Post a Comment