Created
February 24, 2017 09:44
-
-
Save NorthDecoder/ec1b3825d36ac0ae3c0b9f764dce21d7 to your computer and use it in GitHub Desktop.
Installing nodeJS for a single user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# File: Install-NodeJS.sh | |
# Desc: A script to install NodeJS in a subdirectory named node_v | |
# on a server. | |
# License: [MIT](https://mit-license.org/) | |
# Usage: | |
# Make a directory to run the script | |
# $ mkdir node_workspaces | |
# Upload the script to the new directory | |
# Make the script executable. | |
# $ chmod 755 Install-NodeJS.sh | |
# Then run it | |
# $ ./Install-NodeJS.sh | |
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
# User changeable variables | |
# Script runner, you will need to manually check to see which | |
# version is the latest for long term support (LTS) by visiting the | |
# nodejs.org website then paste the correct link for your system here | |
linkToNodeJS="http://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.gz" | |
# on the server commandline type 'uname -a' to see the long version | |
# of server info, enter one server bus type in bus, for example: | |
bus="x86_64" # or | |
# bus="i386" # or | |
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = | |
# Make no user changes below here ... | |
# command reference: | |
# http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html | |
fileToExtract=${linkToNodeJS##*/} | |
#echo $HOME | |
workingDir=`pwd` | |
scriptName=`basename "$0"` # script name | |
clear | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# Begin Definition of Functions | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
continueInstall () { | |
# function receives no inputs, however expects | |
# yes/no response from the user | |
# no: exits from script | |
# yes: allows script to proceed | |
proceed="" #initialize | |
while [[ "$proceed" == "" ]]; do | |
printf "Okay to proceed type[yes/no]:" | |
# expect non blank input | |
read proceed | |
case $proceed in | |
n* ) | |
printf "\nAt your request the script exited without doing the install.\n" | |
exit 1 #just end it all right now | |
;; | |
y* ) | |
printf "\nProceeding with the installation.\n\n" | |
break #get out of the the while loop | |
;; | |
* ) | |
printf "\nInvalid entry: $proceed; Usage: yes / no . " | |
;; | |
esac | |
proceed="" #clear it and try again | |
done # / while | |
} # / continueInstall | |
myDownLoader () { | |
# Expect: | |
# the full url to download from | |
# optional target file name | |
# usage: | |
# myDownLoader $sourceURL $targetFileName | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# download | |
local sourceURL="$1" | |
local targetFileName="$2" | |
printf "\npreparing to download file $targetFileName\n" | |
printf "from $sourceURL\n\n" | |
which wget # does it exist? | |
local wgetStatus="$?" # the exit status | |
which curl # does it exist? | |
local curlStatus=$? # exit status | |
debugSwitch=0 # 0 is real downloads | |
# 1 is debugging (no download) | |
if [[ "$debugSwitch" == "1" ]]; then | |
echo "debugging mode, not really downloading" | |
fi | |
if [[ "$debugSwitch" == "0" ]]; then | |
downloadStatus="" | |
if [[ "$wgetStatus" == "0" ]]; then | |
echo "trying wget command" | |
wget $sourceURL | |
downloadStatus=$? | |
elif [[ "$curlStatus" == "0" ]]; then | |
echo "trying curl command" | |
# curl -o targetFileName sourceURL | |
curl -o $targetFileName $sourceURL | |
downloadStatus=$? | |
else | |
echo "Did not find wget or curl in the PATH, therefore" | |
echo "could not download the requested file." | |
fi #endif downloader | |
fi # endif debug switch | |
return $downloadStatus | |
} # / end function myDownLoader | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# / End Definition of Functions | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
printf "\n\nHello, $USER \n\n" | |
echo "You are running script $scriptName and are preparing" | |
echo "to install NodeJS on `hostname` in a subdirectory" | |
echo "named node_v in the current working directory:" | |
printf " $workingDir \n\n" | |
continueInstall | |
# reference: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html | |
#http://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script | |
currentDirectoryName=${PWD##*/} # remove everything up to and including the last slash | |
if [[ "$currentDirectoryName" == "node_v" ]]; then | |
echo "This script is already in a directory named node_v" | |
echo "the directory structure will look like node_v/node_v" | |
continueInstall | |
fi | |
# system check | |
myServer="`uname -a`" # the long version of server info | |
# user defined 'bus' at top of program for example | |
# bus="x86_64" # or | |
# bus="i386" # or | |
# install only on a system identified with bus | |
if [[ $myServer =~ ($bus) ]]; then | |
# continue with the installation | |
echo "On system with $bus." | |
else | |
echo "Expecting $bus type system, could not confirm type match." | |
echo "uname -a:" | |
echo "$myServer" | |
echo "Exiting without installing NodeJS" | |
exit 1 | |
fi | |
# / end system check | |
echo "Making and switching into directory node_v" | |
mkdir node_v | |
cd node_v | |
workingDir=`pwd` | |
echo $workingDir | |
#myDownLoader $sourceURL $targetFileName | |
myDownLoader $linkToNodeJS $fileToExtract | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# shasum check the downloaded file | |
# curl https://nodejs.org/dist/v4.4.5/SHASUMS256.txt -o SHASUMS256.txt | |
theComparisonFileName="SHASUMS256.txt" # by definition from nodejs website | |
getShasumFrom="$(dirname $linkToNodeJS)" | |
urlCheckSum="$getShasumFrom/$theComparisonFileName" | |
# curl $urlCheckSum -o $theComparisonFileName | |
myDownLoader $urlCheckSum $theComparisonFileName | |
# compare the listed sums to the calculated sums of the local files | |
#shasum -a 256 -c SHASUMS256.txt 2>&1 | grep OK | |
sumResult=$(shasum -a 256 -c SHASUMS256.txt 2>&1 | grep OK) | |
if [[ "$sumResult" =~ "OK" ]]; then | |
echo "SHASUMS256: $sumResult" | |
echo "download was okay, sums list no longer needed" | |
rm SHASUMS256.txt | |
else | |
echo "SHASUMS256 does NOT match, NOT okay to proceed" | |
echo "Try changing into node_v directory, then run the commmand" | |
echo "shasum -a 256 -c SHASUMS256.txt 2>&1 | grep OK" | |
fi | |
# / end of shasum check | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
echo "Untarring file $fileToExtract" | |
echo "Into working directory $workingDir" | |
tar -xvzf $fileToExtract --directory=$workingDir --strip-components=1 | |
# if exit code was okay then delete the zip file | |
if [[ "$?" == "0" ]]; then | |
printf "\ntar exited with no erros\n" | |
printf "removing the compressed file $fileToExtract\n\n" | |
rm -rf $fileToExtract | |
fi | |
printf "\n\nA A directory listing:" | |
ls | |
# / end of the untarring | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# create symbolic links for node and npm in the PATH | |
printf "\n\ncreating symbolic links for node and npm\n\n" | |
cd ~ # ET go home | |
mkdir bin # a directory that is probably in the users PATH | |
cd bin | |
ln -s $workingDir/bin/node node | |
ln -s $workingDir/bin/npm npm | |
cd $workingDir | |
nodeVersion=$(node -v) # test it | |
nodeExitResult=$? | |
npmVersion=$(npm -v) | |
npmExitResult=$? | |
if [[ ("$nodeExitResult" == "0") && ("$npmExitResult" == "0")]]; then | |
printf "\n\nNode version $nodeVersion and npm version $npmVersion are installed\n" | |
printf "in $workingDir\n\n" | |
else | |
printf "\n\nThe install may not have completed correctly.\n" | |
printf "The commands 'node -v' and 'npm -v' should return version numbers\n\n" | |
fi | |
# / end creating symbolic links | |
# - - - - - - - - - - - - - - - - - - - - - - - - | |
# remove the functions from bash | |
unset continueInstall | |
unset myDownLoader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment