Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:25:54+00:00 2026-06-14T04:25:54+00:00

In my code to roll you just write r and then hit enter but

  • 0

In my code to roll you just write r and then hit enter but it seems not to read that and go to else that restarts the while loop. The only way to get it to roll is by typing something else than r than it is a (standard_in) 1: parse error.

#!/bin/bash
#this is a game that is two player and it is a race to get to 
#100 before the other player

echo "Player 1 name?"
read p1
echo "Player 2 name?"
read p2
echo "Okay $p1 and $p2. $p1 will go first"
p1s=0
p2s=0
pt=1
while [ $pt -eq 1 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then

die=$(($RANDOM%6+1))

elif [ $die -eq 1 ]; then
p1s=$(echo "$p1s-$count" |bc)
echo "You rolled a 1. Your score is $p1s"
echo "$p2 turn now."
sleep 1
count=0
pt=2

elif [ $die -gt 1 ]; then
p1s=$(echo "$p1s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p1s"
pt=1

else

if [ $choice == s ]; then
echo "Okay $p1 your score is $p1s"
echo "$p2 turn now"
sleep 1
count=0
pt=2

else
if [ $p1s -gt 99 ]; then
echo "$p1 won. $p2 lost"
echo "would you like to play again?"
read again
elif [ $again  yes ]; then
echo "Okay one second."
sleep 1
clear
bash num.sh
elif [ $again == no ]; then
exit

else

pt=1
fi
fi
fi

done

Don’t worry about their not being a second while for the second player. I cut that out because it same exact code just for player two.

Sample input/output

Player 1 name?
guy1
Player 2 name?
guy2
Okay guy1 and guy2. guy1 will go first
roll or stay
r
roll or stay
rr
(standard_in) 1: parse error
You rolled a 5. Your score is 5
roll or stay
roll
(standard_in) 1: parse error
You rolled a 5. Your score is 10
roll or stay
s
(standard_in) 1: parse error
You rolled a 5. Your score is 15
roll or stay
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:25:55+00:00Added an answer on June 14, 2026 at 4:25 am

    After looking at the properly indented code (see ghoti‘s rewritten code segment), I see the top level if-block is

    if [ $choice == r ]; then
        die=... #roll the die
    elif [ $die -eq 1 ]...
    elif [ $die -gt 1 ]...
    else
        do something...
    fi
    

    Problem is, if [ $choice == r ] is true, you will roll the die and skip the rest of elif–else entries. So you will go to the next iteration without doing anything (except for rolling the die)

    One way to fix this is to check $choice and $die as separate if blocks, namely

    if [ $choice == r ]; then
        #roll the die
    else
        #break or something...
    fi
    
    if [ $die -eq 1 ]; then
        #do something
    elif the rest of $die-related checks  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some javascript roll over code that works fine in firefox but when
Code: <?php //initializing script, do not modify session_start(); define('IN_SCRIPT', true); //so that global.php cannot
Consider such code (this is just example not real code): class Foo(url : String)
I'm on a roll today... I have the following code delaring a dependency property
In my C# code I am using TransactionScope because I was told not to
I've had this noted down on some of my code for a while: /**
I know the STL has set_difference , but I need to just know if
Just been looking at a code golf question about generating a sorted list of
This crops up every now and then for me: I have some C# code
So, At my job we use svn to manage our source code, but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.