Bash programming help, please

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator:Moderators

Post Reply
User avatar
Sparkfist
Forum Administrator
Posts:6754
Joined:Tue Apr 20, 2004 7:12 am
Location:Michigan
Contact:
Bash programming help, please

Post by Sparkfist » Wed Jun 06, 2007 7:25 pm

I've run into a wall here guys, as I see it the mathematical syntax it correct, but I keep getting an error. For example of my problem.

Code: Select all

 d6=$[ ( $RANDOM % 6 ) +1 ]
This is the random number generator and it goes 1 thur 6, simulating one six sided die throw.

Code: Select all

r=$(($d6 * 3))
This should take what the random roll and multiply it by 3, simulating 3 six sided die throws.

Now if I use the line for multiplying the die throw by 3 I get an error, Technically I should be right, but I haven't the clue what's wrong.

Oh and one more question, I've run into another syntax error. What's the proper way to call a function from within another? Reason I ask is that I have server functions, one leading to the other and it would help if I knew how to pass the program to the next.

Thanks in advance.

P.S. This is part of a table top RPG NPC character generator application, written in bash (obviously). Thought I'd help add more info to give anyone an idea of the bigger picture. And if anything else causes me problem I hope you can help, I'll add code as I need help with it.
vskid wrote:Nerd = likes school, does all their homework, dies if they don't get 100% on every assignment
Geek = likes technology, dies if the power goes out and his UPS dies too

I am a geek.

User avatar
joevennix
Portablizer
Posts:999
Joined:Sat Jan 14, 2006 9:34 am
Location:On permanent vacation from reality.
Contact:

Post by joevennix » Sun Jul 29, 2007 11:57 am

Arithmetic is picky about using brackets, so:

Code: Select all

#!/bin/bash

d6=$[ ( $RANDOM % 6 ) +1 ]
r=$[ $d6 * 3 ]
echo $r
But if you did it this way, there would be some numbers (11, 13, etc.) that would never get rolled, since you are multiplying one die by 3. Wouldn't it be better to have 3 dice rolled, so you would have 3 different random numbers, then add their results?

Code: Select all

#!/bin/bash

d61=$[ ( $RANDOM % 6 ) +1 ]
d62=$[ ( $RANDOM % 6 ) +1 ]
d63=$[ ( $RANDOM % 6 ) +1 ]
r=$[ $d61 + $d62 + $d63 ]
echo $r
I guess it depends on your requirements.
Image

User avatar
Sparkfist
Forum Administrator
Posts:6754
Joined:Tue Apr 20, 2004 7:12 am
Location:Michigan
Contact:

Post by Sparkfist » Mon Jul 30, 2007 7:38 am

I appreciate you responding but to be honest I was able to resolve all my coding problems before I turned my program in. If you'd like I can post the code, it's nothing very advanced but it's a nice start for what I want.
vskid wrote:Nerd = likes school, does all their homework, dies if they don't get 100% on every assignment
Geek = likes technology, dies if the power goes out and his UPS dies too

I am a geek.

User avatar
joevennix
Portablizer
Posts:999
Joined:Sat Jan 14, 2006 9:34 am
Location:On permanent vacation from reality.
Contact:

Post by joevennix » Tue Jul 31, 2007 1:14 am

Oh crap, when I answered this the other day I just saw your thread kinda near the top and just assumed it was recent, stupid of me. Doh, sorry bout that.
Image

Post Reply