1. 程式人生 > >隨機挑幾個--腳本

隨機挑幾個--腳本

|| bin space ++ exc pac nts cycle choice

#!/bin/bash # students=(Lilei Hanmeimei Lucy Lily Tom Jerry John Polly UncleWang) # ---------------------- premise ---------------------------- [ $# -eq 1 ] || exit [[ "$1" =~ [1-9] ]] || exit [ $1 -ge 1 -a $1 -le 9 ] || exit # ---------------------- Cycle $1 times -------------------- for ((i=1;i<=$1;i++)); do total=${#students[@]} rand=$(expr $RANDOM % $total) choice[${#choice[@]}]=${students[$rand]} unset students[$rand]=${students[$rand]} # --- exclude array student pace ---- # unset unspace for j in ${students[@]}; do [ -z "$j" ] && countinue unspace[${#unspace[@]}]=$j done students=(${unspace[@]}) ####### ####### done echo "Choice student: ${choice[@]}"

隨機挑幾個--腳本