As an exercise, I was trying to "optimize" the code presented for a bash-based game. Unfortunately, I am having issues because I can't seem to build a bash array of boolean values (if/test results) then use those in a subsequent complex logic expression.
I thought I could assign values in this manner:
bmap[0]="dummy"
bmap[1]=$( test ${board[1]} == ${symbol} )
bmap[2]=$( test ${board[2]} == ${symbol} )
bmap[3]=$( test ${board[3]} == ${symbol} )
bmap[4]=$( test ${board[4]} == ${symbol} )
bmap[5]=$( test ${board[5]} == ${symbol} )
bmap[6]=$( test ${board[6]} == ${symbol} )
bmap[7]=$( test ${board[7]} == ${symbol} )
bmap[8]=$( test ${board[8]} == ${symbol} )
bmap[9]=$( test ${board[9]} == ${symbol} )
then use those in this manner
if \
[[ ${bmap[1]} && ${bmap[2]} && ${bmap[3]} ]] ||
[[ ${bmap[4]} && ${bmap[5]} && ${bmap[6]} ]] ||
[[ ${bmap[7]} && ${bmap[8]} && ${bmap[9]} ]] ||
[[ ${bmap[1]} && ${bmap[4]} && ${bmap[7]} ]] ||
[[ ${bmap[2]} && ${bmap[5]} && ${bmap[8]} ]] ||
[[ ${bmap[3]} && ${bmap[6]} && ${bmap[9]} ]] ||
[[ ${bmap[1]} && ${bmap[5]} && ${bmap[9]} ]] ||
[[ ${bmap[3]} && ${bmap[5]} && ${bmap[7]} ]]
then
echo "true"
else
echo "false"
fi
But the error I am getting is
./test_175.sh: line 29: test: ==: unary operator expected
./test_175.sh: line 30: test: ==: unary operator expected
./test_175.sh: line 31: test: ==: unary operator expected
./test_175.sh: line 32: test: ==: unary operator expected
./test_175.sh: line 33: test: ==: unary operator expected
./test_175.sh: line 34: test: ==: unary operator expected
./test_175.sh: line 35: test: ==: unary operator expected
./test_175.sh: line 36: test: ==: unary operator expected
Is there any way to reuse the bmap array values directly as boolean operation results, and not being forced to perform another test referencing those values?