0

OK i am sure this is an easy question but I am having trouble finding the answer. All I am trying to do is have an if statement that says if $stla is greater than -25 or $stla less than -16. I am pretty sure it is just a syntax thing but I am new to bash and havn't had much luck Googling the answer.

I attached my whole script, the variable assignment seems to be working, so you should need to worry about that. If I disable the if statement and just have echo $stlo it will print every $stlo, so I think the variables are correct.

Right now I just have if ["$stla" -lt "-25"] which returns "line 10: [33.63: command not found" So it is treating $stla as a command and not a number. How do I correct that? And once that is corrected how can I make a or condition to include greater than -25?

Sorry for the simple question, let me know if I need to clarify anything.

#!/bin/bash


for file in *.SAC; do

   stla="$(saclst stla f $file)"
   stla=(`echo $stla | awk '{print $2}'`)
   stlo="$(saclst stlo f $file)"
   stlo=(`echo $stlo | awk '{print $2}'`)

   if ["$stla" -gt "-25"]
   then
       echo $stlo
   fi
done
gisuser_14213
  • 65
  • 1
  • 9
  • 2
    just add a space after `[` and before `]` – Serge Sep 17 '16 at 03:44
  • I tried that and all that changed is a space before the number, but the same error. line 10: [: 27.8819: integer expression expected – gisuser_14213 Sep 17 '16 at 03:48
  • Yes, it expects integer expression. Does it say something ambiguous? – Serge Sep 17 '16 at 03:49
  • It's worth to spend some time on reading `man bash` and `man test` – Serge Sep 17 '16 at 03:50
  • Here: http://stackoverflow.com/questions/17945187/compare-negative-numbers-in-bash `[ "$stla" -gt "-25" ]` – Hameed Sep 17 '16 at 03:52
  • Ok, sorry I didn't even read the message...So is the problem that the number is not an integer? if so how do I deal with that? I'll take a look at what you suggested reading so maybe that will help. – gisuser_14213 Sep 17 '16 at 03:57
  • BTW, the `stla=(...)` and `stlo=(...)` parts aren't what you really want. The parentheses there are telling bash to set those variable to arrays (containing a single value) rather than plain variables. Simply removing the parentheses will fix this, but it'd be easier to just add `| awk '{print $2}'` to the original line that sets each one (i.e. `stla="$(saclst stla f $file | awk '{print $2}' )"`). Also, the logic you describe doesn't make sense, because *every* number is greater than -25 or less than -16 (or both, if it's between them). – Gordon Davisson Sep 17 '16 at 04:28
  • Please take a look: http://www.shellcheck.net/ – Cyrus Sep 17 '16 at 06:40
  • Thanks for the info! Yeah, my logic did not make sense, I wasn't thinking that far ahead really. I fixed it and its doing what I want. – gisuser_14213 Sep 17 '16 at 22:04

2 Answers2

1

This works for me

stla=-24
if [[ "$stla" -gt "-25" ]]
then
echo $stla
fi

Please note spaces in if statement, and fixed a typo in echo $stla

If You want to you float:

stla=-24.5
if [[ $(echo "$stla > -25"| bc) -eq 1 ]]
then
echo $stla
fi
Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
  • Ok, that does seem to work if stla=-24. However the actually numbers are floating point. How can I deal with that? – gisuser_14213 Sep 17 '16 at 04:06
  • 1
    @gisuser_14213: If you need to deal with floating point numbers, you need to use something other than Bash — Bash arithmetic is integer arithmetic. Options include: `ksh`, `zsh`, `awk`, Perl, Python, … Any of these could be used, and there are doubtless other options too. – Jonathan Leffler Sep 17 '16 at 04:17
  • Look at my edit – Farhad Farahi Sep 17 '16 at 04:27
  • Thanks! I will indeed stop using bash for things like this, but I was able to get it to work for this occasion. Thanks for the help. – gisuser_14213 Sep 17 '16 at 22:02
  • `awk` is probably your best bet, but you can do this in `bash`. See my answer for a general purpose technique for handling these sorts of numerical comparisons in `bash`. – Eric Sep 17 '16 at 22:23
0

You can use printf and tr to enable fixed-point comparisons:

if [ $(printf "%0.2f" "$stla"|tr -d .) -gt -2500 ]
then
    echo "$stlo"
fi

This works by first converting $stla into an integer. Effectively, it multiplies $stla by 100 by printing it with two decimal places and then stripping the decimal point. You manually multiply the other operand to make the comparison maintain its original meaning.

Eric
  • 1,431
  • 13
  • 14