#!/bin/bash # rolldice -- parse requested dice to roll and simulate those rolls # examples: d6 = one 6-sided die # 2d12 = two 12-sided dice # d4 3d8 2d20 = a 4-side die, 3 8-sided and 2 20-sided dice proc rolldie { setglobal dice = $1 setglobal dicecount = '1' setglobal sum = '0' # first step, break down arg into MdN if test -z $[echo $dice | grep 'd] { setglobal quantity = '1' setglobal sides = $dice } else { setglobal quantity = $[echo $dice | cut -dd -f1] if test -z $quantity { # user specifyed dN not just N setglobal quantity = '1' } setglobal sides = $[echo $dice | cut -dd -f2] } echo "" ; echo "rolling $quantity $sides-sided die" # now roll the dice... while test $dicecount -le $quantity { setglobal roll = $shExpr(' ( $RANDOM % $sides ) + 1 ') setglobal sum = $shExpr(' $sum + $roll ') echo " roll #$dicecount = $roll" setglobal dicecount = $shExpr(' $dicecount + 1 ') } echo I rolled $dice and it added up to $sum } while test $Argc -gt 0 { rolldie $1 setglobal sumtotal = $shExpr(' $sumtotal + $sum ') shift } echo "" echo "In total, all of those dice add up to $sumtotal" echo "" exit 0