Monday, October 10, 2022

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

Issue

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


id='dv4'>

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


Answered By - How about nope
Answer Checked By - Marilyn (WPSolving Volunteer)