0
#!/bin/bash                                                                                                 

RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10

while [ $Bokoblin_HP -ne 0 ]
      echo "Bokblin HP : ${Bokoblin_HP}/30"
do
    read -p "Press A to Attack or Press H to Heal : " action
    if [ $action = "A" ]
    then
        ((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
        echo $Bokoblin_HP
    fi
done

Result :

Bokoblin HP : 30/30
Press A to Attack or Press H to Heal : A
30

I'm doing an operation on Bokoblin_HP and I would like to stop the program when it reach 0, but my variable won't change and still 30.

5
  • Are you sure you are using bash? A shell like dash will treat ((...)) as a nested sub shell, not an arithmetic command. Try Bokoblin_HP=$((Bokoblin_HP - Link_STR)) instead. Commented Oct 18, 2019 at 13:48
  • @chepner Dash would also complain about read -p. The echo in the while condition is the culprit, try it. Commented Oct 18, 2019 at 13:50
  • I agree the echo is a problem, but that doesn't explain why echo $Bokoblin_HP continues to output 30 after the supposed update. Good point about read -P. Commented Oct 18, 2019 at 13:51
  • 1
    (Unless that's a typo in the question.) Commented Oct 18, 2019 at 13:52
  • 1
    How are you actually running the script? Commented Oct 18, 2019 at 13:55

2 Answers 2

1

Just swap two lines make the program work.

Also, be sure to type an upper A

#!/bin/bash                                                                                                 

RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10

while [ $Bokoblin_HP -ne 0 ]
do
    echo "Bokblin HP : ${Bokoblin_HP}/30"
    read -p "Press A to Attack or Press H to Heal : " action
    if [ $action = "A" ]
    then
        ((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
        echo $Bokoblin_HP
    fi
done

Output :

<user>@<machine> ~
└─ $ ./test.sh 
Bokblin HP : 30/30
Press A to Attack or Press H to Heal : A
20
Bokblin HP : 20/30
Press A to Attack or Press H to Heal : A
10
Bokblin HP : 10/30
Press A to Attack or Press H to Heal : A
0
<user>@<machine> ~
Sign up to request clarification or add additional context in comments.

6 Comments

The location of the echo statement is legal and not the problem.
@chepner The echo statement makes the condition for while always return true, no?
@chepner I didn't say that it was not legal.
@BenjaminW. exactly . Legal but buggy. I think the main problem of OP is that he is trying with a a instead of A
Ah, true, although the output suggests the problem is that the variable is never updated, not that the loop condition fails to be false. (You can simply swap the order of the echo and [...], though.)
|
0

Idk how is supposed to work your solution, im using this

Bokoblin_HP=$(($Bokoblin_HP-$Link_STR))

7 Comments

No, that makes no difference.
It works for me, i tested it with your code. Also I was confused for a while, because I was pressing "a", it wants big A with shift, are you pressing shift?
@KheopsHD Please accept the answer if that fixes your issue.
@Huholoman could you explain why ((Bokoblin_HP=$Bokoblin_HP-$Link_STR)) does not work and Bokoblin_HP=$(($Bokoblin_HP-$Link_STR)) does ?
@CorentinLimier Because the OP is almost certainly not actually using bash. ((...)) is setting Bokoblin_HP to the literal string 30-10 in a subshell, and after that subshell exits, the value of Bokoblin_HP is still 30. No arithmetic is being performed in the current shell. $((...)), on the other hand, is a POSIX-compliant features supported by a broader range of shells. (Although I don't know what shell would support read -p like bash does but not have ((...)) as an arithmetic command.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.