0

I have a signal file which contains Distance information(DIST). Based on DIST value I want to execute program commands on signal file. I have tried if statement but unable to get over it.

for i in *sac
do

    DST=`saclhdr -DIST $i`

    if [ "$DST" <= "5000" ] ; then

gsac << EOF
cut b b 1200
r $i
w append .cut
quit
EOF
    fi
done

In the above code say DST=3313.7, If it is less-then or equal to 5000 then perform given commands, if condition not satisfied then don't perform given commands.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
rehman
  • 101
  • 7
  • Not just `<=`, but even `<` isn't a numeric comparison operator in `<` at all, so it neither of them works even for integers, much less floating-point values. See [the POSIX `test` specification](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) for the list of operators guaranteed to work in `[`. (Despite not being part of that standard,`<` is sometimes used for lexicographic comparison, though it needs to be quoted to prevent it from being parsed as a redirection operator; it's never used for numeric comparison in `[`). – Charles Duffy Apr 09 '19 at 15:03
  • Imprecise, but functional: `(( ${DST%.*} <= 5000 )) && gsac < – vintnes Apr 09 '19 at 15:08

1 Answers1

1

Most shells, bash included, don't do floating-point arithmetic. You need an external program to do it.

if [ "$(echo "$DST <= 5000" | bc)" = 1 ]; then
    ...
fi

bc reads an expression from its standard output, evaluates it, and writes the result to standard output. In the case of a comparison, it writes 1 if the comparision is true, and 0 if it is false.

chepner
  • 497,756
  • 71
  • 530
  • 681