#!/bin/bash
#team数组存储所要选取内容
以下添加功能,可以指定挑选组并从文本数据库中读取
- #!/bin/bash
- #DESC: This script can pick out the specific number and no-repeat unit[s] in a set.
- #AUTH: Robin Liu
- #MAIL: liu.binbin27@gmail.com
- #DATE: 2010-12-30
- #USAG: pickup.sh -t N - n N
- #WARN: If you use -t, you must ensure that the -t is ahead of -n.
- #
- # IF_T变量标记是否指定了-t 选项,默认值为0,既未指定,se_lucky将从第一条记录选取指定字段
- IF_T=0
- # flag为标志数组,为避免选取重复项,置0位为未选取,选取后置1
- flag=(0 0 0 0 0 0 0 0 0)
- # team数组存储选取内容所在的集合
- team=()
- # TEAM_FILE文件存储要选取的组的信息,每行为一组,每组9个字段
- TEAM_FILE="./data.txt"
- # init_team从TEAM_FILE中读取记录并存储在数组team
- # Usage: init_team N (N指定记录号)
- init_team() {
- let I=1
- while read LINE;do
- if [ $I -eq $1 ];then
- team=(${LINE[*]})
- break
- else
- I=$[$I+1]
- fi
- done < $TEAM_FILE
- }
- # se_lucky从team中选取指定数量的字段且保证选取出的字段唯一性
- # Usage: se_lucky N (N指定选取数量)
- se_lucky() {
- let T=$1
- while [ $T -gt 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=$[$T-1]
- fi
- done
- }
- # -t N 指定特定记录,-n N指定选取个数
- while getopts "t:n:" SW; do
- case $SW in
- t)
- IF_T=$[$IF_T+1]
- let T_NO=$OPTARG
- if [ $T_NO -le `cat $TEAM_FILE|wc -l` -a $T_NO -gt 0 ];then
- echo "TEAM $T_NO"
- init_team $T_NO
- else
- echo "Please input a right team number(1-4)." && exit 1
- fi
- ;;
- n)
- if [ $IF_T -eq 0 ];then
- init_team 1
- fi
- let P_NO=$OPTARG
- if [ $P_NO -lt 10 -a $P_NO -gt 0 ];then
- se_lucky $P_NO
- else
- echo "Please input a right number(1-9)." && exit 1
- fi
- ;;
- *)
- echo "Please input like this:`basename $0` -t TEAM_NO(1-4) -n PICK_NUMBERS(1-9)" && exit 1
- ;;
- esac
- done