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

Time:09-19

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related