Skip to content

Instantly share code, notes, and snippets.

@Phate6660
Last active May 16, 2021 17:59
Show Gist options
  • Save Phate6660/336b14e03ecb954ad2504cb4d5d61e17 to your computer and use it in GitHub Desktop.
Save Phate6660/336b14e03ecb954ad2504cb4d5d61e17 to your computer and use it in GitHub Desktop.
A dice roll script.
#!/usr/bin/env bash
## A dice roll script.
## Usage: dice amount
## Example: dice 5
## Explanation: It will roll 5 6-sided dice, add their results, and output the dice roll.
##
## Dependencies: bcalc (https://github.com/Phate6660/bcalc), shuf
amount="${1}"
n=0
while true; do
if [ "${n}" == "${amount}" ]; then
break
fi
calc+=("$(("$(shuf -n1 <(seq 1 6))"))")
n=$((n + 1))
done
calc_amount="${#calc[@]}"
n=0
for ((m=0;m<=calc_amount;m++)); do
if [ "${m}" == "${calc_amount}" ]; then
break
else
final+=("${calc[n]}")
if [ $((n + 1)) == "${calc_amount}" ]; then
break
else
final+=("+")
fi
n=$((n + 1))
continue
fi
done
final_calc="${final[*]}"
final_calc="${final_calc// /}"
result="$(bcalc "${final_calc}")"
echo "dice roll: ${result}"
@Phate6660
Copy link
Author

Lol GitHub's syntax highlighting is broken.
This is a tested working script, and highlighting works fine in all of my dev tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment