How to compare a program's version in a shell script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
4












Suppose I want to compare gcc version to see whether the system has the minimum version installed or not.



To check the gcc version, I executed the following



gcc --version | head -n1 | cut -d" " -f4


The output was



4.8.5


So, I wrote a simple if statement to check this version against some other value



if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi


But it throws an error:



[: integer expression expected: 4.8.5


I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?










share|improve this question























  • @123 Nothing happens
    – Abhimanyu Saharan
    May 27 '16 at 14:21







  • 1




    There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
    – n.st
    May 29 '16 at 3:31






  • 1




    Much simpler than using pipes: gcc -dumpversion
    – Victor Lamoine
    Jan 4 '17 at 14:18















up vote
5
down vote

favorite
4












Suppose I want to compare gcc version to see whether the system has the minimum version installed or not.



To check the gcc version, I executed the following



gcc --version | head -n1 | cut -d" " -f4


The output was



4.8.5


So, I wrote a simple if statement to check this version against some other value



if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi


But it throws an error:



[: integer expression expected: 4.8.5


I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?










share|improve this question























  • @123 Nothing happens
    – Abhimanyu Saharan
    May 27 '16 at 14:21







  • 1




    There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
    – n.st
    May 29 '16 at 3:31






  • 1




    Much simpler than using pipes: gcc -dumpversion
    – Victor Lamoine
    Jan 4 '17 at 14:18













up vote
5
down vote

favorite
4









up vote
5
down vote

favorite
4






4





Suppose I want to compare gcc version to see whether the system has the minimum version installed or not.



To check the gcc version, I executed the following



gcc --version | head -n1 | cut -d" " -f4


The output was



4.8.5


So, I wrote a simple if statement to check this version against some other value



if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi


But it throws an error:



[: integer expression expected: 4.8.5


I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?










share|improve this question















Suppose I want to compare gcc version to see whether the system has the minimum version installed or not.



To check the gcc version, I executed the following



gcc --version | head -n1 | cut -d" " -f4


The output was



4.8.5


So, I wrote a simple if statement to check this version against some other value



if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi


But it throws an error:



[: integer expression expected: 4.8.5


I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?







shell-script test version numeric-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 '16 at 23:58









Jeff Schaller

37k1052121




37k1052121










asked May 27 '16 at 14:01









Abhimanyu Saharan

285619




285619











  • @123 Nothing happens
    – Abhimanyu Saharan
    May 27 '16 at 14:21







  • 1




    There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
    – n.st
    May 29 '16 at 3:31






  • 1




    Much simpler than using pipes: gcc -dumpversion
    – Victor Lamoine
    Jan 4 '17 at 14:18

















  • @123 Nothing happens
    – Abhimanyu Saharan
    May 27 '16 at 14:21







  • 1




    There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
    – n.st
    May 29 '16 at 3:31






  • 1




    Much simpler than using pipes: gcc -dumpversion
    – Victor Lamoine
    Jan 4 '17 at 14:18
















@123 Nothing happens
– Abhimanyu Saharan
May 27 '16 at 14:21





@123 Nothing happens
– Abhimanyu Saharan
May 27 '16 at 14:21





1




1




There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
– n.st
May 29 '16 at 3:31




There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings.
– n.st
May 29 '16 at 3:31




1




1




Much simpler than using pipes: gcc -dumpversion
– Victor Lamoine
Jan 4 '17 at 14:18





Much simpler than using pipes: gcc -dumpversion
– Victor Lamoine
Jan 4 '17 at 14:18











3 Answers
3






active

oldest

votes

















up vote
12
down vote



accepted










I don't know if it is beautiful, but it is working for every version format I know.



#!/bin/bash
currentver="$(gcc -dumpversion)"
requiredver="5.0.0"
if [ "$(printf '%sn' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
echo "Greater than or equal to 5.0.0"
else
echo "Less than 5.0.0"
fi


(Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)






share|improve this answer






















  • or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
    – iruvar
    May 27 '16 at 15:23






  • 2




    At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
    – Xiong Chiamiov
    May 27 '16 at 23:52






  • 2




    This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
    – phk
    Nov 5 '16 at 10:50







  • 1




    -V is a GNU extension of sort(1) thus this solution is non-portable.
    – stefanct
    Sep 4 '17 at 0:46






  • 1




    @LucianoAndressMartini, see what you think of my edit.
    – Wildcard
    Jan 22 at 23:28

















up vote
1
down vote













Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.



check_linux_version() 
version_good=$(uname -r


You can modify this and use it for gcc version checking.






share|improve this answer






















  • +1 it is compatible with other Unix-like ? (My solution is not)
    – Luciano Andress Martini
    Feb 28 at 18:44


















up vote
0
down vote













We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.



The pattern we used was:



#!/usr/bin/env bash

CC=$(command -v gcc)
GREP=$(command -v grep)

# Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
if [[ -f "/usr/gnu/bin/grep" ]]; then
GREP="/usr/gnu/bin/grep"
elif [[ -f "/usr/linux/bin/grep" ]]; then
GREP="/usr/linux/bin/grep"
elif [[ -f "/usr/xpg4/bin/grep" ]]; then
GREP="/usr/xpg4/bin/grep"
fi

# Check compiler for GCC 4.8 or later
GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4.[8-9]|[5-9].)")
if [[ "$GCC48_OR_LATER" -ne 0 ]];
then
...
fi

# Check assembler for GAS 2.19 or later
GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2.19|2.[2-9]|[3-9])")
if [[ "$GAS219_OR_LATER" -ne 0 ]];
then
...
fi





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285924%2fhow-to-compare-a-programs-version-in-a-shell-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    12
    down vote



    accepted










    I don't know if it is beautiful, but it is working for every version format I know.



    #!/bin/bash
    currentver="$(gcc -dumpversion)"
    requiredver="5.0.0"
    if [ "$(printf '%sn' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
    echo "Greater than or equal to 5.0.0"
    else
    echo "Less than 5.0.0"
    fi


    (Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)






    share|improve this answer






















    • or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
      – iruvar
      May 27 '16 at 15:23






    • 2




      At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
      – Xiong Chiamiov
      May 27 '16 at 23:52






    • 2




      This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
      – phk
      Nov 5 '16 at 10:50







    • 1




      -V is a GNU extension of sort(1) thus this solution is non-portable.
      – stefanct
      Sep 4 '17 at 0:46






    • 1




      @LucianoAndressMartini, see what you think of my edit.
      – Wildcard
      Jan 22 at 23:28














    up vote
    12
    down vote



    accepted










    I don't know if it is beautiful, but it is working for every version format I know.



    #!/bin/bash
    currentver="$(gcc -dumpversion)"
    requiredver="5.0.0"
    if [ "$(printf '%sn' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
    echo "Greater than or equal to 5.0.0"
    else
    echo "Less than 5.0.0"
    fi


    (Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)






    share|improve this answer






















    • or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
      – iruvar
      May 27 '16 at 15:23






    • 2




      At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
      – Xiong Chiamiov
      May 27 '16 at 23:52






    • 2




      This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
      – phk
      Nov 5 '16 at 10:50







    • 1




      -V is a GNU extension of sort(1) thus this solution is non-portable.
      – stefanct
      Sep 4 '17 at 0:46






    • 1




      @LucianoAndressMartini, see what you think of my edit.
      – Wildcard
      Jan 22 at 23:28












    up vote
    12
    down vote



    accepted







    up vote
    12
    down vote



    accepted






    I don't know if it is beautiful, but it is working for every version format I know.



    #!/bin/bash
    currentver="$(gcc -dumpversion)"
    requiredver="5.0.0"
    if [ "$(printf '%sn' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
    echo "Greater than or equal to 5.0.0"
    else
    echo "Less than 5.0.0"
    fi


    (Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)






    share|improve this answer














    I don't know if it is beautiful, but it is working for every version format I know.



    #!/bin/bash
    currentver="$(gcc -dumpversion)"
    requiredver="5.0.0"
    if [ "$(printf '%sn' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
    echo "Greater than or equal to 5.0.0"
    else
    echo "Less than 5.0.0"
    fi


    (Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 23 at 15:24

























    answered May 27 '16 at 14:27









    Luciano Andress Martini

    3,345930




    3,345930











    • or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
      – iruvar
      May 27 '16 at 15:23






    • 2




      At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
      – Xiong Chiamiov
      May 27 '16 at 23:52






    • 2




      This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
      – phk
      Nov 5 '16 at 10:50







    • 1




      -V is a GNU extension of sort(1) thus this solution is non-portable.
      – stefanct
      Sep 4 '17 at 0:46






    • 1




      @LucianoAndressMartini, see what you think of my edit.
      – Wildcard
      Jan 22 at 23:28
















    • or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
      – iruvar
      May 27 '16 at 15:23






    • 2




      At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
      – Xiong Chiamiov
      May 27 '16 at 23:52






    • 2




      This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
      – phk
      Nov 5 '16 at 10:50







    • 1




      -V is a GNU extension of sort(1) thus this solution is non-portable.
      – stefanct
      Sep 4 '17 at 0:46






    • 1




      @LucianoAndressMartini, see what you think of my edit.
      – Wildcard
      Jan 22 at 23:28















    or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
    – iruvar
    May 27 '16 at 15:23




    or you could use sort's own checking facilities if printf '%sn%sn' "$(gcc --version | head -n1 | awk 'print $NF')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable
    – iruvar
    May 27 '16 at 15:23




    2




    2




    At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
    – Xiong Chiamiov
    May 27 '16 at 23:52




    At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1
    – Xiong Chiamiov
    May 27 '16 at 23:52




    2




    2




    This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
    – phk
    Nov 5 '16 at 10:50





    This breaks if there are '%' signs in the print statement. Better replace printf "$requiredvern$currentver" with printf '%sn' "$requiredver" "$currentver".
    – phk
    Nov 5 '16 at 10:50





    1




    1




    -V is a GNU extension of sort(1) thus this solution is non-portable.
    – stefanct
    Sep 4 '17 at 0:46




    -V is a GNU extension of sort(1) thus this solution is non-portable.
    – stefanct
    Sep 4 '17 at 0:46




    1




    1




    @LucianoAndressMartini, see what you think of my edit.
    – Wildcard
    Jan 22 at 23:28




    @LucianoAndressMartini, see what you think of my edit.
    – Wildcard
    Jan 22 at 23:28












    up vote
    1
    down vote













    Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.



    check_linux_version() 
    version_good=$(uname -r


    You can modify this and use it for gcc version checking.






    share|improve this answer






















    • +1 it is compatible with other Unix-like ? (My solution is not)
      – Luciano Andress Martini
      Feb 28 at 18:44















    up vote
    1
    down vote













    Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.



    check_linux_version() 
    version_good=$(uname -r


    You can modify this and use it for gcc version checking.






    share|improve this answer






















    • +1 it is compatible with other Unix-like ? (My solution is not)
      – Luciano Andress Martini
      Feb 28 at 18:44













    up vote
    1
    down vote










    up vote
    1
    down vote









    Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.



    check_linux_version() 
    version_good=$(uname -r


    You can modify this and use it for gcc version checking.






    share|improve this answer














    Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.



    check_linux_version() 
    version_good=$(uname -r


    You can modify this and use it for gcc version checking.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 23 at 0:44

























    answered Jan 22 at 23:15









    Kemin Zhou

    19115




    19115











    • +1 it is compatible with other Unix-like ? (My solution is not)
      – Luciano Andress Martini
      Feb 28 at 18:44

















    • +1 it is compatible with other Unix-like ? (My solution is not)
      – Luciano Andress Martini
      Feb 28 at 18:44
















    +1 it is compatible with other Unix-like ? (My solution is not)
    – Luciano Andress Martini
    Feb 28 at 18:44





    +1 it is compatible with other Unix-like ? (My solution is not)
    – Luciano Andress Martini
    Feb 28 at 18:44











    up vote
    0
    down vote













    We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.



    The pattern we used was:



    #!/usr/bin/env bash

    CC=$(command -v gcc)
    GREP=$(command -v grep)

    # Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
    if [[ -f "/usr/gnu/bin/grep" ]]; then
    GREP="/usr/gnu/bin/grep"
    elif [[ -f "/usr/linux/bin/grep" ]]; then
    GREP="/usr/linux/bin/grep"
    elif [[ -f "/usr/xpg4/bin/grep" ]]; then
    GREP="/usr/xpg4/bin/grep"
    fi

    # Check compiler for GCC 4.8 or later
    GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4.[8-9]|[5-9].)")
    if [[ "$GCC48_OR_LATER" -ne 0 ]];
    then
    ...
    fi

    # Check assembler for GAS 2.19 or later
    GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2.19|2.[2-9]|[3-9])")
    if [[ "$GAS219_OR_LATER" -ne 0 ]];
    then
    ...
    fi





    share|improve this answer
























      up vote
      0
      down vote













      We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.



      The pattern we used was:



      #!/usr/bin/env bash

      CC=$(command -v gcc)
      GREP=$(command -v grep)

      # Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
      if [[ -f "/usr/gnu/bin/grep" ]]; then
      GREP="/usr/gnu/bin/grep"
      elif [[ -f "/usr/linux/bin/grep" ]]; then
      GREP="/usr/linux/bin/grep"
      elif [[ -f "/usr/xpg4/bin/grep" ]]; then
      GREP="/usr/xpg4/bin/grep"
      fi

      # Check compiler for GCC 4.8 or later
      GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4.[8-9]|[5-9].)")
      if [[ "$GCC48_OR_LATER" -ne 0 ]];
      then
      ...
      fi

      # Check assembler for GAS 2.19 or later
      GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2.19|2.[2-9]|[3-9])")
      if [[ "$GAS219_OR_LATER" -ne 0 ]];
      then
      ...
      fi





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.



        The pattern we used was:



        #!/usr/bin/env bash

        CC=$(command -v gcc)
        GREP=$(command -v grep)

        # Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
        if [[ -f "/usr/gnu/bin/grep" ]]; then
        GREP="/usr/gnu/bin/grep"
        elif [[ -f "/usr/linux/bin/grep" ]]; then
        GREP="/usr/linux/bin/grep"
        elif [[ -f "/usr/xpg4/bin/grep" ]]; then
        GREP="/usr/xpg4/bin/grep"
        fi

        # Check compiler for GCC 4.8 or later
        GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4.[8-9]|[5-9].)")
        if [[ "$GCC48_OR_LATER" -ne 0 ]];
        then
        ...
        fi

        # Check assembler for GAS 2.19 or later
        GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2.19|2.[2-9]|[3-9])")
        if [[ "$GAS219_OR_LATER" -ne 0 ]];
        then
        ...
        fi





        share|improve this answer












        We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.



        The pattern we used was:



        #!/usr/bin/env bash

        CC=$(command -v gcc)
        GREP=$(command -v grep)

        # Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
        if [[ -f "/usr/gnu/bin/grep" ]]; then
        GREP="/usr/gnu/bin/grep"
        elif [[ -f "/usr/linux/bin/grep" ]]; then
        GREP="/usr/linux/bin/grep"
        elif [[ -f "/usr/xpg4/bin/grep" ]]; then
        GREP="/usr/xpg4/bin/grep"
        fi

        # Check compiler for GCC 4.8 or later
        GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4.[8-9]|[5-9].)")
        if [[ "$GCC48_OR_LATER" -ne 0 ]];
        then
        ...
        fi

        # Check assembler for GAS 2.19 or later
        GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2.19|2.[2-9]|[3-9])")
        if [[ "$GAS219_OR_LATER" -ne 0 ]];
        then
        ...
        fi






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 at 7:40









        jww

        1,55632161




        1,55632161



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285924%2fhow-to-compare-a-programs-version-in-a-shell-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)