PerlPowerTools icon indicating copy to clipboard operation
PerlPowerTools copied to clipboard

bc: test if

Open briandfoy opened this issue 2 years ago • 3 comments
trafficstars

briandfoy avatar Jun 21 '23 12:06 briandfoy

$ 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

mknos avatar Sep 28 '23 02:09 mknos

Thanks, I'll look at this and integrate it and move on your PR.

briandfoy avatar Sep 28 '23 18:09 briandfoy

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"; }

mknos avatar Oct 06 '23 15:10 mknos