检查当前是否安装了 Node 的最有效方法是什么?

Nic*_*mou 3 shell-script node.js

我正在编写一个脚本,首先检查当前是否安装了 Node,如果没有,它将安装最新版本的 Node。如果已安装,则它将继续执行另一个功能以对其进行更新。

我目前的脚本:

#!/bin/bash

function isNodeInstalled() {
    clear
    echo "Checking if Node is installed ..."
    if command --version node &>/dev/null; then
        echo "Installing Node ..."
        curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
        sudo apt-get install nodejs -y
        echo "Node has been installed."
        sleep 5
        updateNode
    else
        echo "Node has already been installed."
        sleep 5
        updateNode
    fi
}
Run Code Online (Sandbox Code Playgroud)

the*_*fog 8

  if which node > /dev/null
    then
        echo "node is installed, skipping..."
    else
        # add deb.nodesource repo commands 
        # install node
    fi
Run Code Online (Sandbox Code Playgroud)

  • 我会使用内置的 `type -P node` 而不是调用外部程序。 (2认同)