• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

shell program, how to compare greater or less than for string

#!/bin/bash

#Both variable have exactly 5 chars.  How to string compare if one is greater ...
version1="2.1.2"
version2="2.1.3"
0
rgbcof
Asked:
rgbcof
2 Solutions
 
pritamduttCommented:
Please find the sample code for string comparison based on your input below:

if [ "$version1" = "$version2" ] 
then
 echo " Same strings"
else
 echo "Different Strings"
fi

Open in new window


Regards,
0
 
TintinCommented:

#!/bin/bash
version1="2.1.2"
version2="2.1.3"

if [[ $version1 > $version2 ]]
then
     echo "$version1 is greater than $version2"
else
    echo "$version2 is greater than $version1"
fi

Open in new window

0
 
Murugesan NagarajanSubject-matter expert at delivery, implementation, and automation at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
Updated script with
1. checking if the two same versions are compared
2. using a function
2. Checking parameters passed to that function
4. Checking return value from that function

#!/bin/ksh
testVersions ()
{
        if [ $# -eq 0 ] || [ "$1" = "" ] || [ "$2" = "" ]
        then
                echo "      output from testVersions:"
                echo "      Pass two parameters to the function testVersions"
                return 1
        fi
        version1="$1"
        version2="$2"
        if [[ $version1 > $version2 ]]
        then
                echo "      $version1 is greater than $version2"
        elif [[ $version1 < $version2 ]]
        then
                echo "      $version2 is greater than $version1"
        else
                echo "      $version2 $version1 are equal"
        fi
        return 0
}
initVersion="2.1.1"
nextVersion="2.1.2"
count="3"
echo continue the while loop until $count is greater than or equal to 0.
while [ $count -ge 0 ]
do
        if [ $count -eq 3 ]
        then
                echo "   calling testVersions without parameters"
                testVersions
                if [ $? -eq 1 ]
                then
                        echo "   errorr one while calling testVersions without parameters"
                fi
        elif [ $count -eq 2 ]
        then
                echo "   calling testVersions parameters as \"\" \"\""
                testVersions "" ""
                if [ $? -eq 1 ]
                then
                        echo "   errorr two while calling testVersions with parameters as \"\" \"\""
                fi
        elif [ $count -eq 1 ]
        then
                echo "   calling testVersions parameters as 2.1.2 2.1.3"
                testVersions "2.1.2" "2.1.3"
                if [ $? -eq 1 ]
                then
                        echo "   errorr three while calling testVersions as 2.1.2 2.1.3"
                fi
        elif [ $count -eq 0 ]
        then
                echo "   calling testVersions parameters as 2.1.3 2.1.3"
                testVersions "2.1.3" "2.1.3"
                if [ $? -eq 1 ]
                then
                        echo "   errorr four while calling testVersions as 2.1.3 2.1.3"
                fi
        fi
        count=`expr $count - 1`
done

Open in new window


output:
continue the while loop until 3 is greater than or equal to 0.
   calling testVersions without parameters
      output from testVersions:
      Pass two parameters to the function testVersions
   errorr one while calling testVersions without parameters
   calling testVersions parameters as "" ""
      output from testVersions:
      Pass two parameters to the function testVersions
   errorr two while calling testVersions with parameters as "" ""
   calling testVersions parameters as 2.1.2 2.1.3
      2.1.3 is greater than 2.1.2
   calling testVersions parameters as 2.1.3 2.1.3
      2.1.3 2.1.3 are equal

Open in new window

0
 
Murugesan NagarajanSubject-matter expert at delivery, implementation, and automation at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
The posted script will work fine for the following type of function calls

testVersions "2.1.3" ""
testVersions "" "2.1.3"
testVersions "" "2.1.3" ""
testVersions "2.1.3" "" ""

Open in new window

0
 
rgbcofAuthor Commented:
You solved it.
[[ $version1 < $version2 ]]

Thanks,
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now