Shell script calling shell script yields odd results

Hello,

I've been banging my head against the wall trying to figure out how to fix an issue. Any help would be appreciated!

What's happening:

I have a server build script that does stuff like install/configure Nginx, Node.js, etc. When installing Node.js, the build script actually calls another shell script.

#Server build script excerpt
#Node.js
if $nodejs_enabled ; then
    sh $server/installNodejs.sh $nodejs_version
fi

Interestingly, I get different results when the server build script calls the Node.js install script, and when I call the Node.js install script myself.

When the server build script calls it, the Node.js directory isn't copied correctly. Instead of copying the entire Node.js folder into /usr/bin/local/, it copies all of the directories within the Node.js folder there. So if you were to run "ls /usr/bin/local/" you would see "bin, etc" instead of "node-v0.12.2-linux-x64".

When I run the Node.js install script myself, it works fine though.

#installNodejs.sh

#!/bin/sh

#Make sure version isn't blank
if [ "$1" = "" ] ; then
    echo "Usage: installNodejs.sh version"
    echo "Example: installNodejs.sh 0.12.2"
    exit
fi

dir="node-v$1-linux-x64"
gz="$dir.tar.gz"
symlink='/usr/local/node'

#Download Node.js binary
wget http://nodejs.org/dist/v$1/$gz

#Extract it
tar -zxf $gz

#Move it to /usr/bin/local/
sudo mv $dir/ /usr/bin/local/

#Remove old symlink
sudo rm $symlink

#Create new symlink
sudo ln -s /usr/bin/local/$dir/bin/node $symlink

I've tried:

-Removing the trailing slash

-Using cp instead of mv

-Running mv with the -v (verbose) flag. That yielded: ‘node-v0.12.2-linux-x64/’ -> ‘/usr/bin/local/’

Any ideas what might be the issue?

2 Replies

Reading through the script, the behavior you describe could occur as follows. If /usr/bin/local/ doesn't already exist, then sudo mv $dir/ /usr/bin/local/ will rename the extracted node-vX.XX.XX-linux-x64 directory to be /usr/bin/local rather than moving it under /usr/bin/local. Putting mkdir -p /usr/bin/local plus an appropriate chown and/or chmod before the mv command would prevent this. (Sticking a test like [ -d /usr/bin/local ] in there, and only creating the directory if it fails, would be a good idea.)

Note that /usr/local/bin, not /usr/bin/local, is the typical place where custom-built or acquired binaries are usually installed. It wouldn't be a surprise if /usr/bin/local does not already exist on most newly-installed Linux systems.

My guess is that you are running the script manually after the automatic run has already created /usr/bin/local, which is why you are seeing different behavior.

The installNodejs.sh script could also benefit from error checking at each of the steps along the way.

That was it! Thanks so much. I owe you a beer :)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct