I came across a question asking about how to use tests and shortcuts. I tried to put a sample test together, but I'm not getting the results I expect. Below is my test code. My output as follows. I am not sure where I am making my mistake. The original question was how to not have to use a return code variable, but put the call to the function in the if test itself.
Thanks for any insight and explanations of my errant thoughts.
Eric
a = 3.1415926536 : expecting TRUE
(( rc && a == ANS )): f1a
[[ rc -eq 0 && ${a} -eq ${ANS} ]]: f2b
[[ f_test -eq 0 && ${a} -eq ${ANS} ]]: f2
(( f_test == 0 && a == ANS )): f3
(( f_test && a == ANS )): f4
a = 3 : expecting FALSE
(( rc && a == ANS )): f1
[[ f_test -eq 0 && ${a} -eq ${ANS} ]]: f2
(( f_test == 0 && a == ANS )): f3
(( f_test && a == ANS )): f4
#!/usr/bin/env ksh93
# True == 0 or space, False == anything else
#set -o xtrace -o verbose
integer rc
typeset -Fr ANS=3.14159265358979323846
function f_test {
integer rc=0
# do some work here to set rc
return ${rc}
} # f_test
float a=${ANS}
print "\n a = ${ANS} : expecting TRUE\n"
# ---------- ---------- ----------
# use a separate return code variable
# ---------- ---------- ----------
f_test
rc=$?
print -n '(( rc && a == ANS )): '
(( rc && a == ANS )) && print 't1a' || print 'f1a'
print -n '[[ rc -eq 0 && ${a} -eq ${ANS} ]]: '
[[ rc -eq 0 && ${a} -eq ${ANS} ]] && print 't2b' || print 'f2b'
# ---------- ---------- ----------
# But can it work in a test with out a rc variable?
# ---------- ---------- ----------
print -n '[[ f_test -eq 0 && ${a} -eq ${ANS} ]]: '
[[ f_test -eq 0 && ${a} -eq ${ANS} ]] && print 't2' || print 'f2'
print -n '(( f_test == 0 && a == ANS )): '
(( f_test == 0 && a == ANS )) && print 't3' || print 'f3'
print -n '(( f_test && a == ANS )): '
(( f_test && a == ANS )) && print 't4' || print 'f4'
#print -n '(( $( f_test ) && a == ANS )): '
#(( $( f_test ) && a == ANS )) && print 't5' || print 'f5'
#(( $( f_test ) && a == ANS )): ./b.ksh: line 43: && a == ANS : arithmetic syntax error
#print -n '(( $( f_test; return $? ) && a == ANS )): '
#(( $( f_test; return $? ) && a == ANS )) && print 't6' || print 'f6'
#(( $( f_test; return $? ) && a == ANS )): ./b.ksh: line 47: && a == ANS : arithmetic syntax error
a=3.0
print "\n a = ${a} : expecting FALSE\n"
# ---------- ---------- ----------
f_test
rc=$?
print -n '(( rc && a == ANS )): '
(( rc && a == ANS )) && print 't1' || print 'f1'
# ---------- ---------- ----------
print -n '[[ f_test -eq 0 && ${a} -eq ${ANS} ]]: '
[[ f_test -eq 0 && ${a} -eq ${ANS} ]] && print 't2' || print 'f2'
print -n '(( f_test == 0 && a == ANS )): '
(( f_test == 0 && a == ANS )) && print 't3' || print 'f3'
print -n '(( f_test && a == ANS )): '
(( f_test && a == ANS )) && print 't4' || print 'f4'
exit
_______________________________________________ ast-users mailing list [email protected] https://mailman.research.att.com/mailman/listinfo/ast-users
