Bash compare local neovim version to desired version - bash

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:
Check if neovim is even installed.
If it is installed pull the local version and check it against a variable version.
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

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

How to check if vim version 8.1+ is installed

I have the following attempt at a vim script to check to see if it is installed and if its version is recent (8+):
# Check if we have vim version 8.1+ installed
VIM_PATH=$(which vim)
VIM_VERSION=$(vim --version |grep '8.[123]')
if [ -z "$VIM_PATH" || -z "$VIM_VERSION" ]
then
...
fi
Is the above a valid script? What might be a better way to grab the version and check if
Since vim --version isn't very machine-readable, I would be tempted to try
if vim --cmd 'if v:version >= 801 | q | else | cq | fi' ; then
# vim at least 8.1
fi
which uses a snippet of vim script to do the comparison. --cmd runs a command before opening any files or running .vimrc; the cq command exits with an error status, and q exits with a success status (necessary because otherwise vim would want to edit a blank file!). | allows placing multiple vim commands on a single line, like ; in shell.

Bash pip search checking version is contained in standard out

I am trying to do a pip search.
pip search --index https://obfuscated/python-release docker-client
This returns
docker-client (1.3.0) - Client library for Docker Pipeline - 0f7f2d821d09db2f268281c84e298967a6df4b11
I want to be able to search that output string to match the versions in my bash script. For example I would want to grep that standard out to make sure that it is indeed version 1.3.0. How would I go about doing in this in the bash script below.
set -e
if pip search --index https://obfuscated/python-release docker-client; then
echo " hello world"
fi
`));
You could do:
#!/bin/bash
version='1.3.0'
if [ $(pip search --index https://obfuscated/python-release docker-client | grep -c $version) -eq 1 ]
then
echo "OK version $version it is."
else
echo "ERROR: pip failure, or wrong version."
fi
Obviously this will work if version numbers do not appear all over the place. Ex. if pip returned ( 1.3.4, upgrade from 1.3.0 ) it would not work. You will have to work around output format, but based on what you provided, this will work.

gcc and w32api not found

can anybody help me why these packages showed unavailable even they are installed?
I am trying to install NS2.3.5 on windows 10 64bit using cygwin.
as known, the install script of ns will check for required package in cygwin which are installed:
packages_base="gcc gcc-g++ gawk tar gzip make patch perl w32api"
packages_xorg="xorg-server xinit libX11-devel libXmu-devel"
you may notice that I modified the script to check for gcc instead of gcc4 and gcc-g++ instead of gcc4-g++, since the gcc4 is obsolete.
I also run the command gcc -dumpversion and I got the version 4.9.3
the basic command to check the package is:
cygcheck -c gcc
and the expected output is:
Package version Status
gcc-g++ 4.9.3-1 OK
however, the script that checks the packages failed to find gcc and w32api even they are installed. all other packages including gcc-g++ were checked successfully and get the exact version.
Okay,
Tried at my end ( as I already have a cygwin package installed ).
To me, this is more of an issue of "cygcheck" utility than anything else.
At my end too "cygcheck" failed to report details in proper for "gcc", with command "cygcheck -c gcc | grep gcc"
I am suggesting a trick here to over come this, but its just a trick.
In script "install" from "ns-2.35", find a function "test_packages" and change it something like below
test_packages() {
for i in $#; do
echo -n "Checking for ${i}... ";
cygcheck -c ${i} | grep ${i} >/dev/null 2>&1;^M
if [ "$?" -eq "0" ]; then
echo "ok";
else^M
cygcheck -l | grep ${i} >/dev/null 2>&1;
if [ "$?" -eq "0" ]; then
echo "ok";
else^M
echo "NO!";^M
echo "";
echo "Package ${i} is not present on your system.";
echo "";
echo "Please install it using Cygwin's setup.exe";
echo "before trying to install the ns-2 distribution.";
fi;
test_proceed;
fi;
done;
}
Basically rechecking again with "cygcheck -l" after first fail.
This passed the test but, I did not go further with installation.
Also there is a link which explains the installation os ns-2.35 on windows
which could be useful too.
http://www.slideshare.net/TBear76/ns235-installation-3395974
Please try out the 'Nov 2014 update', ns-allinone-2.35_gcc482.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNSGJCZ2YzUGJDVk0/view?usp=sharing
Can use all gcc/g++ versions 4.4.x .. 5.2.0
ns2

Is there a way to tell RVM to use the latest version of Ruby that is at least 2.0.0?

I often do a lot of scripting in Ruby, and sometimes I run these scripts on Jenkins jobs or put them where others can run it locally.
I would love to specify in .rvmrc something like:
Use the most recent version of ruby that is installed
Unless it is less then Ruby 2, in which case fail.
That way I can depend on Ruby 2 language changes (e.g., named parameters), but without forcing the environment running the script to install a new ruby if it already has 2.0.0 or 2.1.1 or 2.1.4 installed.
You should be able to run bash commands in the .rvmrc file. So you could check for the latest version and require it and default to a 2.0.0 version if one isn't found. I am not sure what you have in mind for failing since this file gets loaded as the terminal session is started and interrupting that would not be good.
Here is an example I crafted using rbenv sine I don't have rvm installed.
RV=`rbenv versions | grep -E " 2\.\d+\.\d+\S*" | grep -o -E "2\.\d+\.\d+\S*" | sort | tail -n 1`
if [[ $RV ]]
then echo $RV
else echo "DEFAULT"
fi
This example simply outputs the highest version of 2.x.x ruby it finds else it says DEFAULT. For RVM the following could work in your .rvmrc file although I can't test it myself. I based it on output found in the docs. You might need to adjust a little.
RV=`rbenv versions | grep -E "ruby-2\.\d+\.\d+\S*" | grep -o -E "2\.\d+\.\d+\S*" | sort | tail -n 1`
if [[ $RV ]]
then rvm use $RV
else rvm use 2.0.0
fi

Copying gems from previous version of Ruby in rbenv

I installed Ruby 1.9.3-p286 with rbenv. Now, after installing a newer version (p327), obviously, it doesn't know anything about the GEMs installed with the previous version.
Is it possible to copy Gems from that version to the newer one, so that it won't be needed to download them all again?
You can copy the gems/ and bin/ folders, but this will lead to problems. The files in bin/ have hardcoded paths in them.
I'd recommend reinstalling them, which would be as easy as this:
$ rbenv local 1.9.3-p286
$ gem list | cut -d" " -f1 > my-gems
$ rbenv local 1.9.3-p327
$ gem install $(cat my-gems)
I've been looking at this specifically from the perspective of upgrading and reinstalling without downloading. It's not trivial, and I recommend you do some cleanup of your gems to minimize the amount of processing/installation that needs to be done (e.g., I had five versions of ZenTest installed; I did 'gem cleanup ZenTest' before doing this). Be careful with 'gem cleanup', though, as it removes all but the LAST version: if you need to support an older version of Rails, manually clean up the versions you don't need.
I called this script 'migrate-gems.sh':
#! /bin/bash
if [ ${#} -ne 2 ]; then
echo >&2 Usage: $(basename ${0}) old-version new-version
exit 1
fi
home_path=$(cd ~; pwd -P)
old_version=${1}
new_version=${2}
rbenv shell ${old_version}
declare -a old_gem_paths old_gems
old_gem_paths=($(gem env gempath | sed -e 's/:/ /'))
rbenv shell ${new_version}
for ogp in "${old_gem_paths[#]}"; do
case "${ogp}" in
${home_path}/.gem/ruby*|*/.gem/ruby*)
# Skip ~/.gem/ruby.
continue
;;
esac
for old_gem in $(ls -1 ${ogp}/cache/*.gem); do
gem install --local --ignore-dependencies ${ogp}/cache/${old_gem}
done
done
There are three pieces that make this work:
gem env gempath contains the paths (:-separated) where gems are installed. Because
gems are shared in ~/.gem/ruby, I skip this one.
gem install accepts --local, which forces no network connections.
gem install accepts --ignore-dependencies, which disables dependency checking.
I had a fairly large list of gems to move over today and I didn't want to download from rubygems.org (plus, I needed older versions), so I whipped this up fairly quickly.
For posterity, I wrote rbenv-copy-gems.sh to help me do this. It doesn't meet the criteria of this question (it installs from the Internet, not locally), but it's been useful for me as I upgrade or install new version of Ruby via rbenv install.
Current version pasted below for reference, but I keep the gist up to date as I make improvements.
#!/bin/bash
# copy is a misnomer; it's actually LIST + INSTALL
# --from 2.2.1 [--to other-version-else-whatever-is-currently-set]
#
# TODO: install only most-recent version that's installed in FROM
# TODO: use gem names only from FROM, install latest available version (might be more recent than in FROM)
# TODO: pass arguments to gem command (e.g. --no-document)
CURRENT_VERSION=`rbenv version | cut -d' ' -f1`
GEM_LIST_ARGS="--local"
while [[ $# -gt 0 ]]; do
option="$1"
case $option in
--from)
FROM="$2"
shift
;;
--to)
TO="$2"
shift
;;
esac
shift # past argument or value
done
if [ -z "${FROM}" ]; then
FROM="${CURRENT_VERSION}"
fi
if [ -z "${TO}" ]; then
TO="${CURRENT_VERSION}"
fi
echo ">>> Install gems from v${FROM} to v${TO}"
# Get gems and versions, reformat to GEMNAME:version[,version[...]]
gems=(`RBENV_VERSION=${FROM} gem list ${GEM_LIST_ARGS} | sed -e 's/[\(\)]//g' -e 's/, /,/g' -e 's/ /:/'`)
for geminfo in "${gems[#]}"; do
gem=`echo $geminfo | cut -d: -f1`
versions=(`echo $geminfo | sed -e 's/^.*://' -e 's/,/ /g'`)
for version in "${versions[#]}"; do
installed=`RBENV_VERSION=${TO} gem query -i $gem -v $version`
if [ "${installed}" == "false" ]; then
echo ">>> Installing ${gem} ${version}:"
RBENV_VERSION=${TO} gem install $gem -v $version
else
echo ">>> ${gem} ${version} already installed"
fi
echo ""
done
done

Resources