-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrolldice.sh
More file actions
executable file
·30 lines (24 loc) · 919 Bytes
/
rolldice.sh
File metadata and controls
executable file
·30 lines (24 loc) · 919 Bytes
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
#!/bin/bash
NUMBER_DIE=$1
SIZE_DIE=$2
TOTAL_ONLY=1
[[ -z "$3" ]] && TOTAL_ONLY=0
MAX_ROLLS=500
LARGEST_DIE=100
[[ $SIZE_DIE -eq 2 ]] && echo "You might want to try flipcoin.sh" && exit 1
[[ $SIZE_DIE -le 1 ]] && echo "I don't have a die that small!" && exit 1
[[ $SIZE_DIE -gt $LARGEST_DIE ]] && echo "I don't have a die that big!" && exit 1
[[ $NUMBER_DIE -le 0 ]] && echo "Okay, I just won't roll any dice then." && exit 1
[[ $NUMBER_DIE -gt $MAX_ROLLS ]] && echo "My arm will get tired if I try to roll that many!" && exit 1
[[ $TOTAL_ONLY -eq 0 ]] && echo "Rolling ${NUMBER_DIE}d${SIZE_DIE}"
sum=0
for (( i = 1; i <= $NUMBER_DIE ; i++ ))
do
roll=$RANDOM
let "roll %= $SIZE_DIE" # leave it to be a d6
let "roll += 1" # increment by one (there's no zero on a die)
let "sum += roll"
[[ $TOTAL_ONLY -eq 0 ]] && echo "Roll $i = $roll"
done
[[ $TOTAL_ONLY -eq 0 ]] && echo && echo -n "Total = "
echo $sum