-
-
Save Phate6660/336b14e03ecb954ad2504cb4d5d61e17 to your computer and use it in GitHub Desktop.
A dice roll script.
This file contains hidden or 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
#!/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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lol GitHub's syntax highlighting is broken.
This is a tested working script, and highlighting works fine in all of my dev tools.