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
  • 257,635
  • 38
  • 339
  • 400
rehman
  • 101
  • 7

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
  • 446,329
  • 63
  • 468
  • 610