#!/bin/bash

#team数组存储所要选取内容

team=(yi er san si wu liu qi ba jiu)
#flag为标志数组,为避免选取重复项,置0位为未选取,选取后置1
flag=(0 0 0 0 0 0 0 0 0)
#此为选取函数
se_lucky() {
        let T=1
while [ ! $T -eq 0 ] 
        do
         LUCKY_NU=$[$RANDOM%9]
         if [  ${flag[$LUCKY_NU]} -eq 0 ];then
  echo "How lucky you are.It is you:${team[$LUCKY_NU]}"
  flag[$LUCKY_NU]=$[${flag[$LUCKY_NU]}+1]
           T=0
         fi
       done
}
#可以指定命令参数n ,其后指定一次选取个数
while getopts ":n:" SW; do
    case $SW in
n) let I=$OPTARG
  if [ $I -lt 10 -a $I -gt 0 ];then
echo "running.."
                while [ $I -gt 0 ] 
do
se_lucky
I=$[$I-1]
                done
  else
echo "Please input a right number(1-8)." && exit 1
  fi
;;
\?) echo "Please input like this:`basename $0` -n number(1-9)" && exit 1
        ;;
    esac
done

以下添加功能,可以指定挑选组并从文本数据库中读取

 

 

 
  
  1. #!/bin/bash 
  2. #DESC: This script can pick out the specific number and no-repeat unit[s] in a set
  3. #AUTH: Robin Liu 
  4. #MAIL: liu.binbin27@gmail.com 
  5. #DATE: 2010-12-30 
  6. #USAG: pickup.sh -t N - n N 
  7. #WARN: If you use -t, you must ensure that the -t is ahead of -n. 
  8.  
  9. # IF_T变量标记是否指定了-t 选项,默认值为0,既未指定,se_lucky将从第一条记录选取指定字段 
  10. IF_T=0 
  11.  
  12. # flag为标志数组,为避免选取重复项,置0位为未选取,选取后置1 
  13. flag=(0 0 0 0 0 0 0 0 0) 
  14.  
  15. # team数组存储选取内容所在的集合 
  16. team=() 
  17.  
  18. # TEAM_FILE文件存储要选取的组的信息,每行为一组,每组9个字段 
  19. TEAM_FILE="./data.txt" 
  20.  
  21. # init_team从TEAM_FILE中读取记录并存储在数组team 
  22. # Usage: init_team N (N指定记录号) 
  23. init_team() { 
  24.     let I=1 
  25.     while read LINE;do 
  26.         if [ $I -eq $1 ];then  
  27.             team=(${LINE[*]}) 
  28.             break 
  29.         else 
  30.             I=$[$I+1] 
  31.         fi 
  32.     done < $TEAM_FILE 
  33.  
  34. # se_lucky从team中选取指定数量的字段且保证选取出的字段唯一性 
  35. # Usage: se_lucky N (N指定选取数量) 
  36. se_lucky() { 
  37.     let T=$1     
  38.     while [ $T -gt 0 ]  
  39.     do 
  40.         LUCKY_NU=$[$RANDOM%9] 
  41.         if [  ${flag[$LUCKY_NU]} -eq 0 ];then 
  42.             echo "How lucky you are.It is you:${team[$LUCKY_NU]}" 
  43.             flag[$LUCKY_NU]=$[${flag[$LUCKY_NU]}+1] 
  44.             T=$[$T-1] 
  45.         fi 
  46.     done 
  47.  
  48. # -t N 指定特定记录,-n N指定选取个数 
  49. while getopts "t:n:" SW; do 
  50.     case $SW in 
  51.     t) 
  52.         IF_T=$[$IF_T+1] 
  53.         let T_NO=$OPTARG 
  54.         if [ $T_NO -le `cat $TEAM_FILE|wc -l` -a $T_NO -gt 0 ];then 
  55.             echo "TEAM $T_NO" 
  56.             init_team $T_NO  
  57.         else 
  58.             echo "Please input a right team number(1-4)." && exit 1 
  59.         fi 
  60.     ;; 
  61.     n)  
  62.         if [ $IF_T -eq 0 ];then 
  63.             init_team 1 
  64.         fi 
  65.         let P_NO=$OPTARG 
  66.         if [ $P_NO -lt 10 -a $P_NO -gt 0 ];then 
  67.             se_lucky $P_NO 
  68.         else 
  69.             echo "Please input a right number(1-9)." && exit 1 
  70.         fi 
  71.         ;; 
  72.     *)  
  73.         echo "Please input like this:`basename $0` -t TEAM_NO(1-4) -n PICK_NUMBERS(1-9)" && exit 1 
  74.         ;; 
  75.     esac 
  76. done