Skip to content

Instantly share code, notes, and snippets.

@slashinfty
Last active December 7, 2019 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashinfty/1e5287c82272e52770eda70752d9fac9 to your computer and use it in GitHub Desktop.
Save slashinfty/1e5287c82272e52770eda70752d9fac9 to your computer and use it in GitHub Desktop.
Shell script for rolling dice (#d#+#)
#! /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