Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making the check for update script for my theme

I have 2 text files. First one is called "current.txt" and contains the current version. There is 4.1.1 string in that text file.

Second one is called "latest.txt" and contains the latest version. There is 4.2 string in this text file.

So here is the code

echo "Checking update";
x=$(cat ./current.txt)
y=$(cat ./latest.txt)
if [ "$x" -eq "$y" ]
then
       echo There is version $y update
else
       echo Version $x is the latest version
fi

What it mean is if current.txt is NOT the same with latest.txt then it will say "there is version 4.2 update". If not, it will say "version 4.1.1 is the latest version"

But when I try to run it. I get this error

Checking update
./test.sh: line 4: [: 4.1.1: integer expression expected
Version 4.1.1 is the latest version

So what am I doing wrong with this?

share|improve this question
6  
Use = for strings, instead of -eq (for numbers) – Jeff Schaller Jul 2 '16 at 19:36
1  
Related: unix.stackexchange.com/a/285928/117549 – Jeff Schaller Jul 2 '16 at 19:40
    
Thank you, it worked. And it should be !=, if i use = it will reverse the function – superquanganh Jul 2 '16 at 19:41
2  
A slight optimization: use x=$(< ./current.txt) -- that's builtin, so you don't need to call out to cat (ref here) – glenn jackman Jul 2 '16 at 20:00
up vote 2 down vote accepted

The test command, also named [, has separate operators for string comparisons and integer comparisons:

INTEGER1 -eq INTEGER2

INTEGER1 is equal to INTEGER2

vs

STRING1 = STRING2

the strings are equal

and

STRING1 != STRING2

the strings are not equal

Since your data is not strictly an integer, your test needs to use the string comparison operator. The last realization in the comments was that the "-eq" logic did not match the sense of the if/else echo statements, so the new snippet should be:

...
if [ "$x" != "$y" ]
then
       echo There is version $y update
else
       echo Version $x is the latest version
fi
share|improve this answer

BTW, if you have two version strings (e.g. in $x and $y) you can use printf and GNU sort to find which is newer.

$ x=4.1.1
$ y=4.2.2
$ printf "%s\n" "$x" "$y" | sort -V -r
4.2.2
4.1.1

$ if [ $(printf "%s\n" "$x" "$y" | sort -V -r | head -1) = "$x" ] ; then
  if [ "$x" = "$y" ] ; then
    echo "$x is equal to $y"
  else
    echo "$x is newer than $y"
  fi
else
  echo "$x is older than $y"
fi
4.1.1 is older than 4.2.2
share|improve this answer
    
Thanks, thats what i am looking next – superquanganh Jul 3 '16 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.