-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorteo.sh
More file actions
138 lines (111 loc) · 2.26 KB
/
sorteo.sh
File metadata and controls
138 lines (111 loc) · 2.26 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
detect_sed() {
if sed --version >/dev/null 2>&1
then
SED='sed -i'
else
SED='sed -i ""'
fi
}
program="$(basename "$0")"
reset_list() {
cp list curr
usage
}
count() {
wc -l $@ | awk '{ print $1 }'
}
usage() {
echo >&2 "List ready ($(count curr) regs)"
echo >&2 " run \`$program draw [n]' to draw results"
echo >&2 " run \`$program reset' to reset list"
}
confirm() {
local msg
local n rnd
local var
msg="$1"
n="$(expr $RANDOM \* 100 + $RANDOM)"
n="$(expr $n % "$(count /usr/share/dict/words )" + 1)"
rnd="$(head -n "$n" /usr/share/dict/words | tail -1)"
printf >&2 "%s. To confirm type \`%s': " "$msg" "$rnd"
read var
if test "x$rnd" == "x$var"
then
return 0
fi
return 1
}
draw() {
local draw i
local n rnd
draw="$1"
if test "$(count curr)" -lt "$draw"
then
echo >&2 "No source enough!"
exit 1
fi
detect_sed
for i in $(seq 1 $draw)
do
n="$(expr $RANDOM % "$(count curr)" + 1)"
rnd="$(head -n "$n" curr | tail -n 1)"
$SED "${n}d" curr
echo "$rnd" | awk '{ print "WINRAR: " $2 " " $1 }'
done
echo
usage
}
if test "$#" -ge 1
then
command="$1"
fi
# Check that the link exists
if ! test -r curr
then
# Check attendee list
if ! test -r list
then
echo >&2 "No source list."
exit 2
fi
if test "$command" != "reset"
then
echo >&2 "No current list: run \`$program reset' to start"
exit 2
fi
fi
case "$command" in
reset )
if confirm "Current list exists"
then
reset_list
else
echo >&2 "List not reset"
fi
exit 0
;;
draw )
if test "$#" -ge 2
then
draw="$2";
else
draw=1
fi
if ! echo "$draw" | grep -q ^[0-9][0-9]*$
then
echo >&2 "draw: Incorrect number format \`$draw'!"
exit 1
elif test "$draw" -lt 1
then
echo >&2 "draw: We can't draw zero!"
exit 1
fi
draw $draw
;;
* )
echo >&2 "Unknown command \`$command'"
usage
exit 0
;;
esac