-
Notifications
You must be signed in to change notification settings - Fork 0
/
87-rolldice
49 lines (40 loc) · 1.07 KB
/
87-rolldice
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
#!/bin/bash
# rolldice -- parse requested dice to roll and simulate those rolls
# examples: d6 = one 6-sided die
# 2d12 = two 12-sided dice
# d4 3d8 2d20 = a 4-side die, 3 8-sided and 2 20-sided dice
rolldie()
{
dice=$1
dicecount=1
sum=0
# first step, break down arg into MdN
if [ -z "$(echo $dice | grep 'd')" ] ; then
quantity=1
sides=$dice
else
quantity=$(echo $dice | cut -dd -f1)
if [ -z "$quantity" ] ; then # user specifyed dN not just N
quantity=1
fi
sides=$(echo $dice | cut -dd -f2)
fi
echo "" ; echo "rolling $quantity $sides-sided die"
# now roll the dice...
while [ $dicecount -le $quantity ] ; do
roll=$(( ( $RANDOM % $sides ) + 1 ))
sum=$(( $sum + $roll ))
echo " roll #$dicecount = $roll"
dicecount=$(( $dicecount + 1 ))
done
echo I rolled $dice and it added up to $sum
}
while [ $# -gt 0 ] ; do
rolldie $1
sumtotal=$(( $sumtotal + $sum ))
shift
done
echo ""
echo "In total, all of those dice add up to $sumtotal"
echo ""
exit 0