BASH

Verify Essential System Commands

Create a robust shell script to check if crucial system commands like `node`, `npm`, `git`, or `docker` are installed before running critical development tasks.

#!/bin/bash

# List of commands to check
REQUIRED_COMMANDS=("node" "npm" "git" "docker" "curl")

MISSING_COMMANDS=()

echo "Checking for required system commands..."

for cmd in "${REQUIRED_COMMANDS[@]}"; do
  if ! command -v "$cmd" &> /dev/null; then
    MISSING_COMMANDS+=("$cmd")
  else
    echo "  ✔ $cmd found."
  fi
done

if [ ${#MISSING_COMMANDS[@]} -ne 0 ]; then
  echo ""
  echo "ERROR: The following required commands are missing:"
  for missing_cmd in "${MISSING_COMMANDS[@]}"; do
    echo "  - $missing_cmd"
  done
  echo ""
  echo "Please install them to proceed."
  exit 1
else
  echo ""
  echo "All required commands are installed. Proceeding..."
  # Your main script logic can go here
  # For demonstration, we'll just exit successfully.
  exit 0
fi
How it works: This script iterates through a predefined list of essential commands. For each command, it uses `command -v` to check if it's available in the system's PATH. If any command is missing, it collects them and prints an error message, then exits with a non-zero status code, preventing subsequent operations that rely on those tools. This ensures your development environment is correctly set up before execution.

Need help integrating this into your project?

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

Hire DigitalCodeLabs