如何检查bash脚本中的依赖项

Roh*_*hit 7 linux bash

我想检查一下是否在系统上安装了nodejs.我收到此错误:

错误:找不到命令.

我该如何解决?

#!/bin/bash

if [ nodejs -v ]; then
echo "nodejs found"
else
echo "nodejs not found"
fi
Run Code Online (Sandbox Code Playgroud)

hek*_*mgl 13

你可以使用commandbash内置:

if command -v nodejs >/dev/null 2>&1 ; then
    echo "nodejs found"
    echo "version: $(nodejs -v)"
else
    echo "nodejs not found"
fi
Run Code Online (Sandbox Code Playgroud)