fix errors and portability issues in expr commands

Add double-quotes around all expr(1) left-hand-sides where the
argument is just a variable, to prevent syntax errors when the
variable is empty.

Also avoid the `expr match ...` variant as this is not defined in
POSIX and causes errors in some implementations, including
FreeBSD's.
master
Fraser Tweedale 2014-02-21 20:52:50 +10:00
parent ee6d4ab074
commit 4a5f5203d0
1 changed files with 10 additions and 10 deletions

20
nvm.sh
View File

@ -332,10 +332,10 @@ nvm() {
nvm use $VERSION nvm use $VERSION
if ! has "npm" ; then if ! has "npm" ; then
echo "Installing npm..." echo "Installing npm..."
if [ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]; then if [ "`expr "$VERSION" : '\(^v0\.1\.\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
elif [ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]; then elif [ "`expr "$VERSION" : '\(^v0\.2\.\)'`" != '' ]; then
if [ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]; then if [ "`expr "$VERSION" : '\(^v0\.2\.[0-2]$\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
else else
curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh
@ -379,20 +379,20 @@ nvm() {
;; ;;
"deactivate" ) "deactivate" )
if [ `expr $PATH : ".*$NVM_DIR/.*/bin.*"` != 0 ] ; then if [ `expr "$PATH" : ".*$NVM_DIR/.*/bin.*"` != 0 ] ; then
export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:} export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:}
hash -r hash -r
echo "$NVM_DIR/*/bin removed from \$PATH" echo "$NVM_DIR/*/bin removed from \$PATH"
else else
echo "Could not find $NVM_DIR/*/bin in \$PATH" echo "Could not find $NVM_DIR/*/bin in \$PATH"
fi fi
if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man.*"` != 0 ] ; then if [ `expr "$MANPATH" : ".*$NVM_DIR/.*/share/man.*"` != 0 ] ; then
export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:} export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:}
echo "$NVM_DIR/*/share/man removed from \$MANPATH" echo "$NVM_DIR/*/share/man removed from \$MANPATH"
else else
echo "Could not find $NVM_DIR/*/share/man in \$MANPATH" echo "Could not find $NVM_DIR/*/share/man in \$MANPATH"
fi fi
if [ `expr $NODE_PATH : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ] ; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ] ; then
export NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}${NODE_PATH#*$NVM_DIR/*/lib/node_modules:} export NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}${NODE_PATH#*$NVM_DIR/*/lib/node_modules:}
echo "$NVM_DIR/*/lib/node_modules removed from \$NODE_PATH" echo "$NVM_DIR/*/lib/node_modules removed from \$NODE_PATH"
else else
@ -423,7 +423,7 @@ nvm() {
echo "$VERSION version is not installed yet" echo "$VERSION version is not installed yet"
return 1 return 1
fi fi
if [ `expr $PATH : ".*$NVM_DIR/.*/bin"` != 0 ]; then if [ `expr "$PATH" : ".*$NVM_DIR/.*/bin"` != 0 ]; then
PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin} PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin}
else else
PATH="$NVM_DIR/$VERSION/bin:$PATH" PATH="$NVM_DIR/$VERSION/bin:$PATH"
@ -432,12 +432,12 @@ nvm() {
MANPATH=$(manpath) MANPATH=$(manpath)
fi fi
MANPATH=${MANPATH#*$NVM_DIR/*/man:} MANPATH=${MANPATH#*$NVM_DIR/*/man:}
if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man"` != 0 ]; then if [ `expr "$MANPATH" : ".*$NVM_DIR/.*/share/man"` != 0 ]; then
MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man} MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man}
else else
MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH" MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH"
fi fi
if [ `expr $NODE_PATH : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then
NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules} NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules}
else else
NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH"
@ -461,7 +461,7 @@ nvm() {
echo "$VERSION version is not installed yet" echo "$VERSION version is not installed yet"
return; return;
fi fi
if [ `expr $NODE_PATH : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then
RUN_NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules} RUN_NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules}
else else
RUN_NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" RUN_NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH"