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.
Related
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
I want to be able to run a system command from script
bundle exec rubocop
but only if bundle and rubocop gems installed and exist on a machine. If the checks for the existence of these gems fail, ignore the command and exit.
How is it possible to setup these checks before running the command? Maybe I should use bundle --version and see if the command crashes or not? Thank you in advance.
You can grep your installed gems like this
#!/bin/bash
if ! gem list --local | grep -q 'bundler'; then
echo 'Please install bundler first'
exit 1
fi
if ! gem list --local | grep -q 'rubocop'; then
echo 'Please install rubocop first'
exit 1
fi
bundle exec rubocop
An alternative approach to the one(s) suggested before: testing not if the gems are installed, but if the appropriate commands are available (which is not necessarily the same):
#!/bin/bash
if type bundle >/dev/null 2>&1; then
if type rubocop >/dev/null 2>&1; then
bundle exec rubocop
else
echo "Rubocop seems to be not available"
exit 1
fi
else
echo "Bundler seems to be not available"
exit 1
fi
(this script could be better, for example, to report all the missing commands instead of just the 1st encountered, but it's just a quick sketch to illustrate the idea)
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
I'm running a shell script via capistrano to install rvm and ruby. When running
rvm install ruby-${RUBY_VERSION} 2>&1 > ../log/ruby_install.log
in my script, all output seems to be going to the log file, except for the scrollbar output. that output is being sent back to capistrano and it's flooding the output, and looks horrible.
Is there any way I can hide the progress during the command?
I tried running
alias curl="curl --silent"
before the command, but it doesn't work at all, so I guess the install is happening via some other means.
Try:
gem install rvm-capistrano -v ">=1.3.0.rc11"
It contains code to make curl silent
The answer from #mpapis lead me to the following solution:
# Rename .curlrc if present
if [[ -f $HOME/.curlrc ]]; then
echo "Backing up .curlrc"
mv $HOME/.curlrc $HOME/.curlrc~
fi
# Create a temporary .curlrc configuration file, this prevents curl from flooding the Capistrano output
{
echo "insecure"
echo "silent" # Hide verbose output, it floods the capistrano output
echo "show-error"
} > $HOME/.curlrc
I added the above snippet to my bash script, and at the end, I just restored .curlrc to it's previous state:
rm $HOME/.curlrc
if [[ -f $HOME/.curlrc~ ]]; then
mv $HOME/.curlrc~ $HOME/.curlrc
fi
This is modified from rvm-capistrano, check it out on the original Github Repository.
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