Last active
December 7, 2019 01:27
-
-
Save slashinfty/1e5287c82272e52770eda70752d9fac9 to your computer and use it in GitHub Desktop.
Shell script for rolling dice (#d#+#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
if [[ $# -lt 1 ]]; then | |
echo "missing argument" | |
exit 1 | |
fi | |
RANDOM=$$ | |
roll=0 | |
mod=0 | |
while [[ $1 ]] | |
do | |
if [[ $1 =~ ([0-9]+)d([0-9]+)\+?(\-?[0-9]+)? ]]; then | |
i=0 | |
roll=0 | |
while [[ $i -lt ${BASH_REMATCH[1]} ]] | |
do | |
rand=$(($(($RANDOM%$((${BASH_REMATCH[2]}+1))))+1)) | |
roll=$(($roll+$rand)) | |
let i++ | |
done | |
if [[ -z ${BASH_REMATCH[3]} ]]; then | |
echo $roll | |
else | |
mod=$(($roll+${BASH_REMATCH[3]})) | |
echo "$mod (roll: $roll)" | |
fi | |
else | |
echo "invalid roll" | |
fi | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment