10

I want to check whether nodejs is installed on the system or not. I am getting this error:

Error : command not found.

How can i fix it?

#!/bin/bash

if [ nodejs -v ]; then
echo "nodejs found"
else
echo "nodejs not found"
fi
vishal
  • 2,258
  • 1
  • 18
  • 27
Rohit
  • 2,987
  • 3
  • 25
  • 50

6 Answers6

22

You can use the command bash builtin:

if command -v nodejs >/dev/null 2>&1 ; then
    echo "nodejs found"
    echo "version: $(nodejs -v)"
else
    echo "nodejs not found"
fi
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    Check `help command` for the correct documentation since it is a shell _builtin_. PS: Nowadays I would probably recommend to use the `type` builtin for this because it has more options to in/exclude functions and aliases. – hek2mgl May 20 '19 at 11:30
  • I was trying to use `help`, but my shell just couldn't find it. Just now I realized that actually I've been trying to use it in zsh.. Thanks for the clarification. I've been using the `type` builtin up till now, but just switched it to `command` since people here have upvoted this the most (no clear best practice). I guess I'll switch back to `type`. Is it alright if you update your answer with that comment about `type`, or it doesn't really matter that much? – Fanatique May 20 '19 at 11:35
  • It doesn't really fit into this post, because the OP is asking how to check if nodejs is installed on their OS. `command` is the right tool for the job here because it doesn't include shell functions. (`type -f` would work as well, but it's a little more complicated) – hek2mgl May 20 '19 at 11:39
2

The name of the command is node, not nodejs

which returns the path to the command to stdout, if it exists

if [ $(which node 2>/dev/null) ]; then
  echo "nodejs found"
else
  echo "nodejs not found"
fi
edi9999
  • 19,701
  • 13
  • 88
  • 127
  • don't use `which` in scripts. – Leonhardt Wille Jul 05 '19 at 08:30
  • 1
    For anyone (like myself) who wonders the exact reason to avoid `which`, there's lots more information here: https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script – Alex W Dec 10 '19 at 18:54
2

This is not what the OP asked for (nearly 3 years ago!), but for anyone who wants to check multiple dependencies:

#!/bin/bash
echo -n "Checking dependencies... "
for name in youtube-dl yad ffmpeg
do
  [[ $(which $name 2>/dev/null) ]] || { echo -en "\n$name needs to be installed. Use 'sudo apt-get install $name'";deps=1; }
done
[[ $deps -ne 1 ]] && echo "OK" || { echo -en "\nInstall the above and rerun this script\n";exit 1; }

Here's how it works. First, we print a line saying that we are checking dependencies. The second line starts a "for name in..." loop, in which we put the dependencies we want to check, in this example we will check for youtube-dl, yad and ffmpeg. The loop commences (with "do") and the next line checks for the existence of each command using the bash command "which." If the dependency is already installed, no action is taken and we skip to the next command in the loop. If it does need to be installed, a message is printed and a variable "deps" is set to 1 (deps = dependencies) and then we continue to the next command to check. After all the commands are checked, the final line checks to see if any dependencies are required by checking the deps variable. If it is not set, it appends "OK" to the line where it originally said "Checking dependencies.... " and continues (assuming this is the first part of a script). If it is set, it prints a message asking to install the dependencies and to rerun the script. It then exits the script.

The echo commands look complicated but they are necessary to give a clean output on the terminal. Here is a screenshot showing that the dependencies are not met on the first run, but they are on the second.

Bash dependency check script screen print

PS If you save this as an script, you will need to be in the same directory as the script and type ./{name_of_your_script} and it will need to be executable.

Scooby-2
  • 167
  • 4
  • 1
    don't use `which` in scripts. – Leonhardt Wille Jul 05 '19 at 08:30
  • 4
    @Leonhardt Wille - This is an excellent community for people to learn from others. Making a statement such as "don't use which in scripts" without qualification, reasons or suggestions does not add anything useful to the post. I see you made the exact same comment to another post. I presume you are referring to the poor portability of which. However, in bash - note that my example specifies a bash shell with #!/bin/bash on the 1st line - it ignores the pitfalls of having builtins or functions of the same name. The OP wants to know if a command is installed. My script does exactly that. – Scooby-2 Jul 06 '19 at 13:55
  • 1
    For anyone (like myself) who wonders the exact reason to avoid `which`, there's lots more information here: https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script – Alex W Dec 10 '19 at 18:53
1

You may check the existence of a program or function by

type nodejs &>/dev/null || echo "node js not installed"

However, there is a more sophisticated explanation available here.

Community
  • 1
  • 1
Manuel Barbe
  • 2,104
  • 16
  • 21
0

I was thinking about this and came up with a few versions, then went on the internet to see what others have to say and ended up here. Albeit an old thread, I'll reply with my thoughts.

First to answer the OP's original question: How can i fix it?

if node -v &>/dev/null; then
    echo "nodejs found"
else
    echo "nodejs not found"
fi

If you are simply checking if node works, this would do it. But it isn't a very generic way to do it.

Another way is to use command in a loop and collect the missing dependencies (in this example looking for the commands kind and kubectl).

for app in kind kubectl; do command -v "${app}" &>/dev/null || not_available+=("${app}"); done
(( ${#not_available[@]} > 0 )) && echo "Please install missing dependencies: ${not_available[*]}" 1>&2 && exit 1

Or less concisely expressed:

unset not_available # script safety, however not necessary.

for app in kind kubectl; do
    if ! command -v "${app}" &>/dev/null; then
        not_available+=("${app}")
    fi
done

if (( ${#not_available[@]} > 0 )); then
    echo "Please install missing dependencies: ${not_available[@]}" 1>&2
    exit 1
fi

Then I figured I'd want a way to do the same without a loop, so came up with this:

not_installed=$(command -V kind kubectl 2>&1 | awk -F': +' '$NF == "not found" {printf "%s ", $(NF-1)}')
[[ -n ${not_installed} ]] && echo "Please install missing dependencies: ${not_installed}" 1>&2 && exit 1

The command -V can take any number of entries and posts the result back to stdout and stderr (though I redirect both to stdout for the next command to parse).

awk sets the field separator to <colon><one or more space>, expressed as : +. If the last field contains, "not found", print the second to last field, being the name of the command which is not installed.

Lastly, if the variable contains any data, then report back which dependencies that are missing to stderr and exit the script!


You can do dependency checks in a million ways, but here are a few alternatives which are more generally applicable and not too lengthy while still being easy to follow. :]

Kaffe Myers
  • 424
  • 3
  • 9
-1

If all you want is to check to see if a command exists, use which command. It returns the patch if the command is called, and nothing if it is not found

if [ "$(which openssl)" = "" ] ;then
echo "This script requires openssl, please resolve and try again."
exit 1
fi
meanderer
  • 15
  • 1
  • don't use `which` in scripts. – Leonhardt Wille Jul 05 '19 at 08:29
  • For anyone (like myself) who wonders the exact reason to avoid `which`, there's lots more information here: https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script – Alex W Dec 10 '19 at 18:54