4

I am writing a script that will first check if Node is currently installed, if not it will install the latest version of Node. If it is installed, then it will proceed to another function to update it.

My current script:

#!/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
}

2 Answers 2

10
  if which node > /dev/null
    then
        echo "node is installed, skipping..."
    else
        # add deb.nodesource repo commands 
        # install node
    fi
1
  • 2
    I would use the builtin type -P node rather than calling out to an external program. Commented Jun 8, 2016 at 12:31
0

-P (only supports in bash) using type command.

#!/bin/bash
if type node > /dev/null 2>&1 && which node > /dev/null 2>&1 ;then
    node -v
    echo "node is installed, skipping..."
else
    echo "install node"
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.