這是bash 版
fang@bash ~
$ cat bin/marksix
#! /bin/bash
# marksix genertor, Usage: $0 [ [option] argument ]
#
twf_cc@yahoo.com.hk
output=""
base_array=({1..49})
length=${#base_array[@]}
argument=$1
howmany=${argument:-6}
usage="Usage:\t${0##*/} [[option] number of picks](default 6)"
message_to_user="Number of picks should not be greater than $length"
out_of_range=66
non_numberic_argument=65
if [[ $howmany =~ [^0-9]+ ]]
then
echo -e "$usage" >&2 ; exit $non_numberic_argument
elif [ $howmany -gt $length ]
then
echo "$message_to_user" >&2 ; exit $out_of_range
fi
for (( i=1 ; i<=howmany ; i++ ))
do
pick=${base_array[$(( RANDOM % length ))]}
output="$output $pick"
resize_array=($(echo ${base_array[@]} | sed "s/ $pick//")
base_array=(${resize_array[@]})
length=${#base_array[@]}
done
sort_result=$( printf '%s\n' $output | sort -n )
echo -e ${sort_result//\n/" "}
這是Perl 版
fang@bash ~
$ cat bin/marksix.pl
#! /usr/bin/perl
# marksix generator, perl version
#
twf_cc@yahoo.com.hk
@num=qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
42 43 44 45 46 47 48 49) ;
$length=@num ;
$output="" ;
$howmany=$ARGV[0] ;
$i=1 ;
die "Number of picks should not be greater than $length\n" if ($howmany > $length) ;
die "Usage:\t$0 [[option]number of picks](default 6)\n" if ($howmany =~ /\D+/) ;
$howmany = 6 if (@ARGV == 0) ;
while ($i <= $howmany){
srand ;
$random=int(rand() * $length * $i) % $length ;
$pick=$num[$random] ;
$output="$output $pick" ;
splice (@num, $random, 1) ;
$length=@num ;
$i++ ;
}
@out=split(" ", $output) ;
@sorted=sort {$a <=> $b} @out ;
print "@sorted\n"
這是 ash + GNU grep 版
fang@bash ~
$ cat bin/lotto
#! /bin/ash
# Usage: $0 [ [option] argument ]
#
twf_cc@yahoo.com.hk
# set -x
output=
draw="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 \
38 39 40 41 42 43 44 45 46 47 48 49"
length=`echo $draw | tr ' ' '\n' | wc -l`
argument=$1
howmany=${argument:-6}
usage="Usage: `basename $0` [[option] number of picks](default 6)"
message="Number of picks should not be greater than $length"
non_numberic=65
out_of_range=66
step=1
if echo "$howmany" | grep '[^0-9][^0-9]*' > /dev/null 2>&1
then
echo "$usage" 1>&2
exit $non_numberic
fi
if [ $howmany -gt $length ]
then
echo "$message" 1>&2
exit $out_of_range
fi
while [ $step -le $howmany ]
do
random=`awk -v remainder=$length -v base=$$ -v step=$step '
BEGIN{
srand()
print int(rand() * base * step ) % remainder + 1
}'`
pick=`echo $draw | tr ' ' '\n' | head -n"$random" | tail -n1`
output="$output $pick"
resize_draw=`echo $draw | sed -e "s/ $pick//"`
draw="$resize_draw"
length=`echo $draw | tr ' ' '\n' | wc -l`
step=`expr $step + 1`
done
echo $output | tr ' ' '\n' | sort -n | tr '\n' ' '
echo ""
screen shot
fang@bash ~
$ uname -a
CYGWIN_NT-5.1 bash 1.5.25(0.156/4/2) 2007-12-14 19:21 i686 Cygwin
fang@bash ~
$ marksix 89
Number of picks should not be greater than 49
fang@bash ~
$ marksix 89j
Usage: marksix [[option] number of picks](default 6)
fang@bash ~
$ marksix 49j
Usage: marksix [[option] number of picks](default 6)
fang@bash ~
$ marksix.pl 49j
Usage: /home/fang/bin/marksix.pl [[option]number of picks](default 6)
fang@bash ~
$ marksix.pl 89j
Number of picks should not be greater than 49
fang@bash ~
$ marksix.pl 89
Number of picks should not be greater than 49
fang@bash ~
$ lotto 49j
Usage: lotto [[option] number of picks](default 6)
fang@bash ~
$ lotto 89j
Usage: lotto [[option] number of picks](default 6)
fang@bash ~
$ lotto 89
Number of picks should not be greater than 49
fang@bash ~
$
fang@bash ~
$ bash --version | head -n2
GNU bash, version 3.2.33(1-release (i686-pc-cygwin)
Copyright (C) 2007 Free Software Foundation, Inc.
fang@bash ~
$ perl --version | head -n2
This is perl, v5.8.8 built for cygwin-thread-multi-64int
fang@bash ~
$ grep --version | head -n2
grep (GNU grep) 2.5.1
Perl 不同於其他程式的 regexp engine? /\D+/ 該等如 /[^0-9]+/ or /[^0-9][^0-9]*/ 的呀?