Home > Back-end >  Bash compare local neovim version to desired version
Bash compare local neovim version to desired version

Time:11-15

I am working on a script to determine if my local neovim version is below a desired version. I am using bash on PopOS 22.04.

The script will be used for a dev environment setup and lunarvim requires the Neovim version to be 0.8.0 .

The desired end result of the script would do three things:

  1. Check if neovim is even installed.
  2. If it is installed pull the local version and check it against a variable version.
  3. If neovim is an older version it will uninstall it. Another part of the script runs an ansible playbook to add the unstable neovim repository then install neovim afterwards.

I have tried various iterations of using ansible, dpkg, neovim -v, and even trying to shorten the output of neovim -v. Any help is appreciated.

The bash version below is the latest variation of the comparison I have tried. I am running into the error if neovim is not installed it will error out on line three with nvim: command not found (expected error). Afterwards it will print out the final echo statement (unexpected output).

#!/bin/bash
has_nvim=$(command -v nvim >/dev/null)
nvim_version=$(nvim --version | head -1 | grep -o '[0-9]\.[0-9]')
if ! $has_nvim; then
  echo "Nvim is not installed"
elif [ $(echo $nvim_version >= 0.9 | bc -l) ]; then
     echo "Wrong version of Nvim is installed"
     sudo apt remove neovim -y
else
     echo "Nvim version 0.9 or greater is installed"
fi

CodePudding user response:

You must add a if/else case not to get Nvim version if not installed.

A fixed version of your code :

#!/bin/bash

command -v nvim >/dev/null

if [[ $? -ne 0 ]]; then
    echo "Nvim is not installed"
else
    nvim_version=$(nvim --version | head -1 | grep -o '[0-9]\.[0-9]')

    if (( $(echo "$nvim_version < 0.9 " |bc -l) )); then
            echo "Wrong version of Nvim is installed"
            sudo apt remove neovim -y
    else
        echo "Nvim version 0.9 or greater is installed"
    fi
fi
  • Related