Search code examples
shellubuntush

Write if condition in shell script and run command based on ubuntu version


Suppose for Ubuntu version 18 or less than that, i want to run command A else command B How to do that?


Solution

  • You can get version form lsb_release command and then check if major version is less or equal -le to 18. If lsb_release is not avaiable in a system then just cat /etc/os-release and grep from it what is needed.

    #!/bin/bash
    
    OS_VER=$(lsb_release -sr | cut -d'.' -f1)
    
    if [[ $OS_VER -le 18 ]]; then
        echo "command 1"
    else
        echo "command 2"
    fi