package='widdershins'
if [ `npm list -g | grep -c $package` -eq 0 ]; then
npm install $package --no-shrinkwrap
fi
alternative including the directory check:
package='widdershins'
if [ `npm list -g | grep -c $package` -eq 0 -o ! -d node_module ]; then
npm install $package --no-shrinkwrap
fi
Explaination:
npm list -g
lists all installed packages
grep -c $package
prints a count of lines containing $package (which is substituted to 'widdershins' in our case)
-eq
is an equals check, e.g. $a -eq $b
returns true if $a is equal to $b, and false otherwise.
-d
checks if the given argument is a directory (returns true if it is)
!
is the negation operator, in our case is negates the -d result
-o
is the logical or operator
To sum it up:
- First code: if the $package is installed, then the -eq result is false and this causes the if statement to be false. If $package is not installed, then the -eq result is true (and the if statement is also true).
- Second code: in addition to description of first code, if node_module is a directory, then the if statement is false. If node_module is not a directory then the if statement is true. And this is independend from the -eq result because of the logical or connection.
This could also help you.