centos7 shell脚本一键安装nodejs

post by rocdk890 / 2020-3-4 14:30 Wednesday linux技术
好了,python的一键安装有了,当然nodejs也得有个一键安装,毕竟这两个东西是大家经常会用到,有需要的小伙伴可以拿去.
系统:centos 7.x(64位)

cat /root/soft_shell/auto_install_node.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to initialization OS."
    exit 1
fi

nodeversion="12.13.0"
VERSIONPATTERN="[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}"

echo -n "Please input a  node version number (Enter 12.13.0): "
read customVersion

if [ ! -z $customVersion ]
then
    macthResult=$(echo $customVersion | grep -E -x $VERSIONPATTERN )
    if [ -z $macthResult ]
    then
        echo "Please input a  right version number. eg. 10.15.0 or 12.13.0"
        exit 1
    fi
    nodeversion=$customVersion
fi

number=`echo $nodeversion|awk -F '.' '{print $1}'`

if [ ! -s node-v$nodeversion-linux-x64.tar.xz ];then
	wget https://npm.taobao.org/mirrors/node/latest-v$number.x/node-v$nodeversion-linux-x64.tar.xz
fi

echo -e "\033[32m \nIn the installation... \033[0m"

if [ ! -s /usr/bin/node ];then
	tar xf node-v$nodeversion-linux-x64.tar.xz
	mv node-v$nodeversion-linux-x64/ /usr/local/node
	ln -s /usr/local/node/bin/node /usr/bin/node
	ln -s /usr/local/node/bin/npm /usr/bin/npm
else
	rm -f /usr/bin/node
	rm -f /usr/bin/npm
	rm -fr /usr/local/node
	tar xf node-v$nodeversion-linux-x64.tar.xz
	mv node-v$nodeversion-linux-x64/ /usr/local/node
	ln -s /usr/local/node/bin/node /usr/bin/node
	ln -s /usr/local/node/bin/npm /usr/bin/npm
fi

#if [ ! -s /etc/yum.repos.d/yarn.repo ];then
#	wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
#	rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
#	yum -y install yarn
#	yarn config set registry https://registry.npm.taobao.org

	npm config set registry https://registry.npmmirror.com --global
	npm config set disturl https://npmmirror.com/mirrors/node --global
#fi

echo -e "\nInstalled node and npm version is ... "
node -v && npm -v

echo -e "\033[32m \nInstall Successfully! \033[0m"
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-768.html

标签: centos 安装 shell install 一键 nodejs

评论: