skip to Main Content

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

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. You can get the version of OS from any of these two file:

    • /etc/lsb_release
    • /etc/os-release

    Then have an if/else command accordingly to check the version.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search