2
0

magento-version 637 B

123456789101112131415161718192021
  1. #!/usr/bin/env bash
  2. # Method 1: Using bin/magento --version
  3. version=$(bin/magento --version --no-ansi 2> /dev/null | cut -d" " -f 3)
  4. if [[ -z "$version" ]]; then
  5. # Method 2: Using grep in composer.lock
  6. version=$(grep -A 1 "magento/magento2-base" ./src/composer.lock | grep "version" | awk -F "\"" '{print $4}')
  7. fi
  8. if [[ -z "$version" ]]; then
  9. # Method 3: Using bin/yq in composer.json
  10. version=$(bin/yq -oj '.version' ./src/composer.json | sed 's/"//g')
  11. fi
  12. # If version is still not obtained, output error message
  13. if [[ -z "$version" ]]; then
  14. echo "Failed to retrieve Magento version."
  15. else
  16. echo "$version"
  17. fi