1

This might be the most common question, as it has been asked many times but I also tried different ways e.g used -lt or without {}

But I still get confused. I am using multiple "&&" conditions in IF but it seems like my script does not follow these conditions and simply does the task (copy the files) even if it does not fulfill if condition. All of these variables used in the below lines are float or negative numbers.

if [[ "${time}" > 1 ]] && [[ "${time}" < "10" ]] && [[ "${s_arr}" > "0" ]] && [[ 
   "${stlo}" < "105" ]] && [[ "${stlo}" > "104.2" ]]
then
    cp $event $output/$day'_'$event'.sac'
fi
  • Can you provide some initialization with which we can reproduce the problem? – Lajos Arpad Mar 28 '22 at 15:44
  • Sure, let me modify this question. – user16941410 Mar 28 '22 at 15:45
  • 2
    if you float numbers then you can't use shell arithmetic comparisons. BTW, check the output of `[[ 2 > 1000 ]] && echo yes` – Fravadona Mar 28 '22 at 15:47
  • 1
    @Fravadona it prints yes. Does it mean I should use **while** or **bc**? – user16941410 Mar 28 '22 at 15:50
  • 1
    Don't use `[[ ... ]]` for arithmetic comparisons at all. Use `((...))` instead (either you have it available, or you don't have `[[ ... ]]` either.) – chepner Mar 28 '22 at 15:54
  • 1
    Just like with `[`, `[[` uses `>` et al. for string comparisons and `-gt` et al. for integer comparisons. `((...))` doesn't do string comparisons *at all*, so it's free to use `>` for integer comparisons. – chepner Mar 28 '22 at 15:55
  • 3
    And yes, you must use an external tool like `bc` to do floating-point comparisons. – chepner Mar 28 '22 at 15:56
  • 1
    BTW, you're quoting the wrong thing in your code. You don't need to quote constant strings like `_`, whereas you _do_ need to quote expansions to prevent string-splitting and globbing. `cp "$event" "$output/${day}_$event.sac"` would be better, but you could also write `cp "$event" "$output/$day"_"$event".sac` and it would be just as correct. Run your code through http://shellcheck.net/ and follow the links in the warnings for a full explanation. – Charles Duffy Mar 28 '22 at 16:07

0 Answers0