PerlPowerTools
PerlPowerTools copied to clipboard
bc: test if
trafficstars
$ cat bc.sh
#!/bin/sh
BC='perl bc'
bc()
{
code="$1"
echo "$code" | $BC | fgrep -c ok
}
for tst in \
'if (0 == 0) { print "ok"; }' \
'if (1 != 0) { print "ok"; }' \
'if (1 > 0) { print "ok"; }' \
'if (0 < 1) { print "ok"; }' \
'a=2; if (a == 2) { print "ok"; }' \
'a=2; if (a == 2 && 1 == 1) { print "ok"; }' \
; do
ok=$(bc "$tst")
echo "SUCCESS=$ok -- $tst"
if [ $ok != 1 ]; then
echo ABORT
exit 1
fi
done
echo 'ALL OK'
exit 0
$ ./bc.sh
SUCCESS=1 -- if (0 == 0) { print "ok"; }
SUCCESS=1 -- if (1 != 0) { print "ok"; }
SUCCESS=1 -- if (1 > 0) { print "ok"; }
SUCCESS=1 -- if (0 < 1) { print "ok"; }
SUCCESS=1 -- a=2; if (a == 2) { print "ok"; }
SUCCESS=0 -- a=2; if (a == 2 && 1 == 1) { print "ok"; }
ABORT
Thanks, I'll look at this and integrate it and move on your PR.
SUCCESS=0 -- a=2; if (a == 2 && 1 == 1) { print "ok"; }
I didn't investigate why this was happening. But now I think it could be an order of operation problem because adding more parens makes it evaluate to true:
a=2; if ((a==2) && (1==1)) { print "ok"; }