-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_pick.sh
More file actions
executable file
·110 lines (99 loc) · 2.14 KB
/
random_pick.sh
File metadata and controls
executable file
·110 lines (99 loc) · 2.14 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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
RESET='\033[0m'
if [ $# -gt 1 ]; then
echo -e "Usage : ./random_pick.sh [title]"
exit 1
fi
#set title
if [ $# -eq 1 ]; then
title="$1"
else
title="Random Pick"
fi
length=0
for ((i=0; i<${#title}; i++))
do
if echo "${title:i:1}" | grep -qE '[가-힣]'
then
length=$(($length+2))
else
length=$(($length+1))
fi
done
#set candidates
while true; do
clear
candis=$(( ${#candidates[@]} - 1))
echo -e $GREEN"@___"$title"___@"
for index in $(seq 0 $candis); do
if [ $index -gt -1 ]; then
echo "$((index + 1)): ${candidates[index]}"
fi
done
echo -en "@___"
for ((i=0; i<$length; i++)); do
echo -en "_"
done
echo -e "___@"$RESET
echo -e "> : proceed, < [number] : delete candidate"
read candidate
if [ "$candidate" == ">" ]; then
break
elif [ $(echo $candidate | cut -d' ' -f1) == "<" ]; then
del=$(echo $candidate | cut -d' ' -f2)
unset candidates[$(expr $del - 1)]
declare -a candidates=("${candidates[@]}")
echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
clear
else
candidates+=("$candidate")
fi
done
#get random indexes
clear
rand=$(seq 0 $(expr ${#candidates[@]} - 1) | sort -R)
#get number to pick
while true; do
clear
echo -e $GREEN"@___"$title"___@"
for index in "${!candidates[@]}"; do
echo "$((index+1)): ${candidates[index]}"
done
echo -en "@___"
for ((i=0; i<$length; i++)); do
echo -en "_"
done
echo -e "___@"$RESET
echo -e "Type the number of winners : "
read number
#check validity
if expr "$number" + 0 2> /dev/null >/dev/null && [ "$number" -lt "$(expr ${#candidates[@]} + 1)" ]; then
break
fi
done
#show result
clear
echo -e $GREEN"@___"$title"___@"
for index in "${!candidates[@]}"; do
echo "$((index+1)): ${candidates[index]}"
done
echo -en "@___"
for ((i=0; i<$length; i++)); do
echo -en "_"
done
echo -e "___@"$RESET
echo -e $CYAN"Winners are .."$YELLOW
declare -a rand_array=($rand)
for i in $(seq 0 $(expr $number - 1)); do
sleep 1
index=${rand_array[i]}
echo "$((index+1)): ${candidates[index]}"
done
exit 0