#!/bin/bash
# Get the total memory available
total_memory=$(free -m | grep 'Mem:' | awk '{print $2}')
# Get the used memory
used_memory=$(free -m | grep 'Mem:' | awk '{print $3}')
# Get the free memory
free_memory=$(free -m | grep 'Mem:' | awk '{print $4}')
# Calculate the percentage of used memory
used_percentage=$(echo "scale=2; $used_memory * 100 / $total_memory" | bc)
# Print the memory usage information
echo "Total Memory: ${total_memory} MB"
echo "Used Memory: ${used_memory} MB"
echo "Free Memory: ${free_memory} MB"
echo "Used Memory Percentage: ${used_percentage}%"
Save the above script in a file, for example, check_memory_usage.sh. Make the script executable using the following command:
chmod +x check_memory_usage.sh
To run the script, simply execute:
./check_memory_usage.sh
0 comments:
Post a Comment