MacOSX下如何用Bash脚本检查可用磁盘空间是否超40GB?
Absolutely, you can write a Bash script to check if your available disk space exceeds 40GB using df combined with awk—and avoiding the human-readable format headaches is key here. Parsing df -h directly gets messy with units like G or M, so let's break down the most reliable approaches:
Recommended Method: Byte-Level Calculation (No Unit Confusion)
The easiest way to avoid unit parsing errors is to get df to output space in raw bytes, then compare against the 40GB threshold (converted to bytes). This uses integer arithmetic, which is straightforward in Bash.
Here's a complete, reusable script snippet:
#!/bin/bash # Define the mount point you want to check (e.g., / or /mnt/backup) TARGET_MOUNT="/" # Calculate 40GB in bytes (40 * 1024^3) THRESHOLD=$((40 * 1024 * 1024 * 1024)) # Get available bytes for the target mount point # df -B1 sets block size to 1 byte; awk skips the header line and grabs the 4th column (available space) AVAILABLE_BYTES=$(df -B1 "$TARGET_MOUNT" | awk 'NR == 2 {print $4}') # Compare available space to threshold if [ "$AVAILABLE_BYTES" -gt "$THRESHOLD" ]; then echo "✅ Sufficient disk space: $((AVAILABLE_BYTES / 1024 / 1024 / 1024)) GB available (needs >40GB)" # Proceed with database download/restore operations here else echo "❌ Insufficient disk space: only $((AVAILABLE_BYTES / 1024 / 1024 / 1024)) GB available (needs >40GB)" exit 1 fi
How this works:
df -B1 "$TARGET_MOUNT": Forcesdfto output all values in raw bytes, eliminating unit ambiguity.awk 'NR == 2 {print $4}': Skips the header row (NR == 2targets the second line of output) and extracts the 4th column, which represents available disk space.- Bash handles integer comparison directly with
-gt(greater than), so no external tools likebcare needed.
Alternative: Parsing Human-Readable Output (More Complex)
If you prefer to use df -h, you can parse the unit suffix (G/M/K) and convert to a common unit for comparison. Note this is less reliable due to potential unit variations across systems:
#!/bin/bash TARGET_MOUNT="/" REQUIRED_GB=40 # Get available space (e.g., "45G" or "30000M") AVAILABLE=$(df -h "$TARGET_MOUNT" | awk 'NR == 2 {print $4}') # Extract numeric value and unit VALUE=${AVAILABLE%?} UNIT=${AVAILABLE: -1} # Convert to GB and compare case "$UNIT" in G) if (( $(echo "$VALUE > $REQUIRED_GB" | bc -l) )); then echo "✅ Sufficient space: $VALUE GB available" else echo "❌ Insufficient space: $VALUE GB available (needs >$REQUIRED_GB GB)" exit 1 fi ;; M) if (( $(echo "$VALUE / 1024 > $REQUIRED_GB" | bc -l) )); then echo "✅ Sufficient space: $((VALUE / 1024)) GB available" else echo "❌ Insufficient space: $((VALUE / 1024)) GB available (needs >$REQUIRED_GB GB)" exit 1 fi ;; K) if (( $(echo "$VALUE / 1024 / 1024 > $REQUIRED_GB" | bc -l) )); then echo "✅ Sufficient space: $((VALUE / 1024 / 1024)) GB available" else echo "❌ Insufficient space: $((VALUE / 1024 / 1024)) GB available (needs >$REQUIRED_GB GB)" exit 1 fi ;; *) echo "⚠️ Unsupported unit in df output: $UNIT" exit 1 ;; esac
Note:
This method requires bc for floating-point math and has edge cases (e.g., systems using lowercase units like g instead of G). Stick to the byte-level approach for consistent, error-free results.
内容的提问来源于stack exchange,提问作者prax0r

