Reduce usage of `cd' to avoid issues with shell aliases

Since we run in interactive context the builtin `cd' may have been aliased to
print some additional info when invoked, thus it's safer for us to avoid
parsing the output from subshells which calls it.

For instance in .bash_profile I have the following function to redefine
`cd' such that it will list the destination directory contents:
cd () { builtin cd "$@" && ls -F --color=auto }
master
Emanuele Aina 2013-04-11 19:57:56 +02:00
parent f1b484ee87
commit 42915fc21c
1 changed files with 37 additions and 29 deletions

66
nvm.sh
View File

@ -7,7 +7,7 @@
# Auto detect the NVM_DIR # Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then if [ ! -d "$NVM_DIR" ]; then
export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) && pwd) export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd)
fi fi
# Make zsh glob matching behave same as bash # Make zsh glob matching behave same as bash
@ -16,6 +16,16 @@ if [ ! -z "$(which unsetopt 2>/dev/null)" ]; then
unsetopt nomatch 2>/dev/null unsetopt nomatch 2>/dev/null
fi fi
function nvm_set_nullglob {
if type setopt > /dev/null 2>&1; then
# Zsh
setopt NULL_GLOB
else
# Bash
shopt -s nullglob
fi
}
# Obtain nvm version from rc file # Obtain nvm version from rc file
function rc_nvm_version { function rc_nvm_version {
if [ -e .nvmrc ]; then if [ -e .nvmrc ]; then
@ -69,7 +79,7 @@ nvm_ls()
if [[ "$PATTERN" == v?*.?*.?* ]]; then if [[ "$PATTERN" == v?*.?*.?* ]]; then
VERSIONS="$PATTERN" VERSIONS="$PATTERN"
else else
VERSIONS=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 1.2,1n -k 2,2n -k 3,3n` VERSIONS=`nvm_set_nullglob; basename -a $NVM_DIR/v${PATTERN}* 2>/dev/null | sort -t. -k 1.2,1n -k 2,2n -k 3,3n`
fi fi
if [ ! "$VERSIONS" ]; then if [ ! "$VERSIONS" ]; then
echo "N/A" echo "N/A"
@ -244,21 +254,22 @@ nvm()
t="$VERSION-$os-$arch" t="$VERSION-$os-$arch"
url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz" url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz"
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-${t}.tar.gz | awk '{print $1}'` sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-${t}.tar.gz | awk '{print $1}'`
local tmpdir="$NVM_DIR/bin/node-${t}"
local tmptarball="$tmpdir/node-${t}.tar.gz"
if ( if (
mkdir -p "$NVM_DIR/bin/node-${t}" && \ mkdir -p "$tmpdir" && \
cd "$NVM_DIR/bin" && \ curl -C - --progress-bar $url -o "$tmptarball" && \
curl -C - --progress-bar $url -o "node-${t}.tar.gz" && \ nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum && \
nvm_checksum `${shasum} node-${t}.tar.gz | awk '{print $1}'` $sum && \ tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \
tar -xzf "node-${t}.tar.gz" -C "node-${t}" --strip-components 1 && \ mv "$tmpdir" "$NVM_DIR/$VERSION" && \
mv "node-${t}" "../$VERSION" && \ rm -f "$tmptarball"
rm -f "node-${t}.tar.gz"
) )
then then
nvm use $VERSION nvm use $VERSION
return; return;
else else
echo "Binary download failed, trying source." >&2 echo "Binary download failed, trying source." >&2
cd "$NVM_DIR/bin" && rm -rf "node-${t}.tar.gz" "node-${t}" rm -rf "$tmptarball" "$tmpdir"
fi fi
fi fi
fi fi
@ -272,6 +283,8 @@ nvm()
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
make='gmake' make='gmake'
fi fi
local tmpdir="$NVM_DIR/src"
local tmptarball="$tmpdir/node-$VERSION.tar.gz"
if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | grep '200 OK'`" != '' ]; then if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | grep '200 OK'`" != '' ]; then
tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz"
sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-$VERSION.tar.gz | awk '{print $1}'` sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-$VERSION.tar.gz | awk '{print $1}'`
@ -280,12 +293,11 @@ nvm()
fi fi
if ( if (
[ ! -z $tarball ] && \ [ ! -z $tarball ] && \
mkdir -p "$NVM_DIR/src" && \ mkdir -p "$tmpdir" && \
cd "$NVM_DIR/src" && \ curl --progress-bar $tarball -o "$tmptarball" && \
curl --progress-bar $tarball -o "node-$VERSION.tar.gz" && \ if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum; fi && \
if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} node-$VERSION.tar.gz | awk '{print $1}'` $sum; fi && \ tar -xzf "$tmptarball" -C "$tmpdir" && \
tar -xzf "node-$VERSION.tar.gz" && \ cd "$tmpdir/node-$VERSION" && \
cd "node-$VERSION" && \
./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \ ./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \
$make && \ $make && \
rm -f "$NVM_DIR/$VERSION" 2>/dev/null && \ rm -f "$NVM_DIR/$VERSION" 2>/dev/null && \
@ -327,15 +339,11 @@ nvm()
t="$VERSION-$os-$arch" t="$VERSION-$os-$arch"
# Delete all files related to target version. # Delete all files related to target version.
(mkdir -p "$NVM_DIR/src" && \ rm -rf "$NVM_DIR/src/node-$VERSION" \
cd "$NVM_DIR/src" && \ "$NVM_DIR/src/node-$VERSION.tar.gz" \
rm -rf "node-$VERSION" 2>/dev/null && \ "$NVM_DIR/bin/node-${t}" \
rm -f "node-$VERSION.tar.gz" 2>/dev/null && \ "$NVM_DIR/bin/node-${t}.tar.gz" \
mkdir -p "$NVM_DIR/bin" && \ "$NVM_DIR/$VERSION" 2>/dev/null
cd "$NVM_DIR/bin" && \
rm -rf "node-${t}" 2>/dev/null && \
rm -f "node-${t}.tar.gz" 2>/dev/null && \
rm -rf "$NVM_DIR/$VERSION" 2>/dev/null)
echo "Uninstalled node $VERSION" echo "Uninstalled node $VERSION"
# Rm any aliases that point to uninstalled version. # Rm any aliases that point to uninstalled version.
@ -430,15 +438,15 @@ nvm()
"alias" ) "alias" )
mkdir -p $NVM_DIR/alias mkdir -p $NVM_DIR/alias
if [ $# -le 2 ]; then if [ $# -le 2 ]; then
(cd $NVM_DIR/alias && for ALIAS in `\ls $2* 2>/dev/null`; do for ALIAS in $(nvm_set_nullglob; echo $NVM_DIR/alias/$2* ); do
DEST=`cat $ALIAS` DEST=`cat $ALIAS`
VERSION=`nvm_version $DEST` VERSION=`nvm_version $DEST`
if [ "$DEST" = "$VERSION" ]; then if [ "$DEST" = "$VERSION" ]; then
echo "$ALIAS -> $DEST" echo "$(basename $ALIAS) -> $DEST"
else else
echo "$ALIAS -> $DEST (-> $VERSION)" echo "$(basename $ALIAS) -> $DEST (-> $VERSION)"
fi fi
done) done
return return
fi fi
if [ ! "$3" ]; then if [ ! "$3" ]; then