Ubuntu安装node

#!/bin/bash
echo "添加环境变量需要root权限,如无root权限,则不添加环境变量"
echo "输入Node下载地址(目前仅支持Node官方网站上Linux Binaries下载项目或输入1使用默认下载)"
read getUrl
if [ $getUrl = "1" ] 
then
  getUrl="https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.xz"
fi
if [ $(expr match ${getUrl} http) = "0" -a $(expr match ${getUrl} https) = "0" ] 
then
  getUrl="https://"$getUrl
fi

nodeSourceName=$(echo ${getUrl} | awk 'BEGIN {FS="/"} {print $NF}')
wget ${getUrl}
echo "下载完成,开始解压"
xz -d ${nodeSourceName}
nodeSourceName=`echo ${nodeSourceName} | sed 's/.xz//g'`
tar -xvf ${nodeSourceName}
nodeSourceName=$(echo ${nodeSourceName} | sed 's/.tar//g')
if [ $(id -u) = "0" ] 
then
  nodePath="export PATH="$(pwd)"/"${nodeSourceName}"/bin:"${PATH}
  cp /etc/profile /etc/profile.bak
  echo "已备份/ect/profile文件,备份文件为/etc/profile.bak"
  sed -i '$a '"${nodePath}"'' /etc/profile
  echo "请使用 source /etc/profile 命令完成环境变量设置"
else
  echo "不是root用户"
  exit 0
fi

node安装脚本,初学shell的产物,大神请勿嫌弃。 执行完成后使用 source /etc/profile即可(shell里我没用成功)

原文地址:https://www.cnblogs.com/wofeiwofei/p/5497707.html