how-much-swap.sh 711 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. IFS=$'\n\t'
  4. # Function to calculate swap space
  5. calculate_swap() {
  6. local ram=$1
  7. if (( ram <= 2 )); then
  8. echo "$((ram * 2)) GB"
  9. elif (( ram > 2 && ram <= 8 )); then
  10. echo "$ram GB"
  11. elif (( ram > 8 && ram <= 64 )); then
  12. echo "4 GB"
  13. else
  14. echo "4 GB (Hibernation not recommended)"
  15. fi
  16. }
  17. # Main script
  18. echo "Enter the amount of RAM in GB: "
  19. read ram_amount
  20. # Check if input is numeric
  21. if ! [[ "$ram_amount" =~ ^[0-9]+$ ]]; then
  22. echo "Error: Input must be a positive integer."
  23. exit 1
  24. fi
  25. # Call function to calculate swap space
  26. swap_space=$(calculate_swap $ram_amount)
  27. echo "Recommended swap space: ${swap_space}"