BASH

Validate Presence of Essential CLI Tools in Bash

Ensure your Bash scripts have all necessary command-line tools installed (e.g., node, npm, git) before execution, providing robust error handling.

#!/bin/bash

REQUIRED_COMMANDS=("node" "npm" "git" "docker")

for cmd in "${REQUIRED_COMMANDS[@]}"; do
  if ! command -v "$cmd" &> /dev/null; then
    echo "Error: Required command '$cmd' is not installed. Please install it to proceed."
    exit 1
  fi
done

echo "All required commands are installed. Proceeding..."
How it works: This script iterates through a predefined list of `REQUIRED_COMMANDS`. For each command, it uses `command -v "$cmd" &> /dev/null` to check if the executable is available in the system's PATH. If any required command is missing, an informative error message is printed to stderr, and the script exits immediately with a non-zero status, preventing further execution with incomplete dependencies.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs