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
}

Comments

Popular posts from this blog

Xubuntu Home Server on Dell XPS 13 9370

Cygwin + syslog-ng

Installing Fedora 21 on a (late 2014) Mac Mini