Search code examples
bashmatharithmetic-expressions

Variable containing number not seen as one in arithmetic function


I'm writing a small script. He has 2 purposes:

Check if the number saved in both files is less than 60, then do nothing.

ELSE - divide the first number by the second number, multiply it by 100 and check if the result is greater than 20, if it is, run diff on the files and save the result to a file.

Im in the first part of the script. My current code:

#!/bin/bash

function timeDiff() {

local time1=$(cat $1)
local time2=$(cat $2)

  echo $time1
  echo $time2 

  if (( "$time1" < 60 )) && (( "$time2" < "60" )); then     

  echo No comparision needed.       

  else      
    diff $1 $2      

  fi
}

timeDiff 1/1.txt 2/1.txt

the contains of the file 1.txt are:

1
(one empty line here)

the contains of the file 2.txt are:

2
(one empty line here)

For this script I get console output like this:

1
2
 < 60 ")syntax error: invalid arithmetic operator (error token is "
1c1
< 1
---
> 2

How do I make this script working? I've tried everything...


Solution

  • Ok, finally... all the code was correct... the only problem was that I am working on Windows, using Cygwin. I made both 1.txt with Notepad...

    if (( "$time1" < 60 )) && (( "$time2" < 60 )); then
    

    Is working perfecly. The problem was the

    \r
    

    In the TXT files.

    Running dos2unix on both files fixed everything.