Solving issues with signed and unsigned numbers in BASH.
For a start, Yes, BASH is type independent – I know that. My problem lays in catching executables output into a BASH variable.
My executables are not quite UNIX compatible, where returned values are 0 for OK, >0 ERROR. They return 0 for OK, >0 WARNING (only, so move on) and <0 ERROR (abort) instead.
Code:
// C++ BIN A
int main(){
// some code...
return 1;
}
// C++ BIN B
int main(){
return -1;
}
// C++ BIN C
int main(){
return 255;
}
Below is the part of a test suite in BASH to verify the output. I compile and execute the A, B and C, binaries in a loop as $mybinary.
Code:
#!/bin/bash
# (…)
# here is a piece of the loop
$mybinary
RETURN=$?
if (( $RETURN >= 0 )); then
let testOk=$testOk+1
RMSG=OK
else
let testFail=$testFail+1
RMSG=ERROR
fi
echo [ $RETURN ] RESULT: $RMSG
# (...)
The test programs A, B, and C should return 1, -1 and 255 respectively. A is fine, 1 is returned. The problem is that programs B and C both return 255 value. I know -1 is hex 0xff (signed) which is equivalent to 0xff (unsigned) and is back-equal to decimal 255.
The question is, how to determine a real decimal 255 value from a negative -1 in BASH?
Is the '$?' 8-bit aligned (dynamically shortened) when no higher values expected?
Have to mention that, I'm considering a use of “cout << mynumber;” before each last “return mynumber” and grab the output with RETURN=`mybinary`, from the screen. Yes, it works but looks ugly.