0

I'm currently working for a little game card project with c++ but I need some stats, here I would like to make an average of the number of round before a victory. I try bash to do it but I have a little error and I'm pretty new to bash.

here is the code :

#!/bin/bash
i="1"
moyenne="1"

while [ $i -le 40 ]
do
    moyenne = $(($moyenne + ./a.out 2>&1 | tail -1))
    ((i++))
done

and there is my error

./script.sh: line 7: 1 + ./a.out 2>&1 | tail -1: syntax error: operand expected (error token is "./a.out 2>&1 | tail -1")

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
Pablo Clsn
  • 13
  • 1
  • 6
  • You want to use `$(./a.out 2>&1 | tail -1)`. – paddy Mar 23 '17 at 23:06
  • ok i change my line with this `avg = $(($avg + $(./a.out 2>&1 | tail -1)))` but I'm getting this error now ... `./script.sh: line 7: avg: command not found` – Pablo Clsn Mar 23 '17 at 23:11
  • http://shellcheck.net/ would be a good place to start. (You can't have spaces around the `=` in an assignment, which that -- or any other good static checking tools -- would point out). – Charles Duffy Mar 23 '17 at 23:13
  • ...by the way, do you want floating-point results here? Native bash is probably not the best idea, then; see [BashFAQ #22](http://mywiki.wooledge.org/BashFAQ/022). – Charles Duffy Mar 23 '17 at 23:13
  • Nice for the spaces between `=` thanks a lot @CharlesDuffy I'm gonna look for the floating problem thanks a lot ! – Pablo Clsn Mar 23 '17 at 23:15
  • @PabloClsn I wrote a really long answer to your question including a solution with floating point arithmetic. But since the question was closed I can't send it. To not waste my effort if you want it I can email it to you. – Itay Grudev Mar 23 '17 at 23:34

1 Answers1

1

Two problems: The spaces around the equal sign -- Bash is sensitive about this -- and the way you add (+) the two operands without evaluating with $().

I don't know exactly what your a.out returns, but substituting it with a simple echo 1, this adds up to 41:

moyenne=$(($moyenne + $(echo 1 2>&1 | tail -1)))
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65