George Jor George Jor - 1 month ago 14
Bash Question

function to detect cli installation

I have inline checking to detect the installation of cli packages to save time on installing existing package, but I found it is tedious and not that readable for those long list.

For example:

which -s redis-cli || brew install redis
which -s java || brew cask install java
which -s yarn || npm install -g yarn


Are there any function to make it nice looking? For example:

function npmInstall(name) {
if (which -s name) {
return;
}

npm install -g name;
}


Thanks a lot!

Answer

You may pass client packages as parameters.

Example, script.sh:

for cli in $@; do
  which "$cli" || npm install -g "$cli"
done

invoked with ./script.sh redis java yarn