2

working with a legacy system, here I'm connecting with sqlplus to store the returned value in a variable and its worked fine with me but the problem with compearing with floating values a numeric/floating comparison and the returned value might be 0.052 or 0.0032 or integer value, as far as I know, Bash doesn't support float comparison also bc does not support in POSIX

isubsCount=$( sqlplus -s user/pass <<-EOF
    set pagesize 0;
    set feedback off;
    set verify off;
    set heading off echo off;
    select  to_char((Max(start_time_timestamp+ (2/24))- p.port_statusmoddat), 999.999) Diff
    from  test
    exit;
EOF )


if [ ${isubsCount%.*} -ge 1 ]; then

echo yes

fi

also i have tried this but it does not work,:

if (( $(echo "$isubsCount >= 1" |bc -l) )); then

echo yes

fi
4
  • thank you, as far as i know Bash doesn't support float comparison also bc does not supported in posix
    – yong shi
    Mar 27, 2020 at 19:22
  • 2
    bc is part of the POSIX spec (see here). Are you working in an environment that doesn't have it? Does it have awk or perl? Also, should the title be about "comparison" instead of "compression"? Mar 27, 2020 at 20:11
  • yes it has perl and awk
    – yong shi
    Mar 27, 2020 at 20:13
  • bc is a mandatory utility so it must exist. If it's somehow missing in a non-standard system then just use dc
    – phuclv
    Mar 28, 2020 at 16:58

1 Answer 1

2

This will work using any awk in any shell on all UNIX boxes:

$ isubsCount=0.052
$ awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}'
$ echo $?
1
$ if awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}'; then echo "yes"; else echo "no"; fi
no

$ isubsCount=1
$ awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}'
$ echo $?
0
$ if awk -v val="$isubsCount" 'BEGIN{exit !(val >= 1)}'; then echo "yes"; else echo "no"; fi
yes

Obviously you could change the exit statement from exit !(val >= 1) to exit (val < 1) to get the same result but I wrote it as I did just to show how you'd get the exit status you want without having to write the opposite of the condition you actually want to test for.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .