如何在Bash中获取当前的可用磁盘空间?

时间:2014-01-13 08:51:41

标签: bash if-statement diskspace

我正在运行一些不断耗尽磁盘空间的操作。出于这个原因,我希望我的计算机在磁盘空间低于2GB时发出声音。我知道我可以通过运行df -h

获得列出可用磁盘空间的输出
Filesystem                                      Size   Used  Avail Capacity   iused     ifree %iused  Mounted on
/dev/disk1                                     112Gi  100Gi   12Gi    90%  26291472   3038975   90%   /
devfs                                          191Ki  191Ki    0Bi   100%       663         0  100%   /dev
map -hosts                                       0Bi    0Bi    0Bi   100%         0         0  100%   /net
map auto_home                                    0Bi    0Bi    0Bi   100%         0         0  100%   /home

但我不能在if-then语句中使用此输出,以便在可用空间低于2GB时播放声音。

有人知道如何才能获得可用空间而不是这个完整输出吗?

3 个答案:

答案 0 :(得分:3)

首先,可用磁盘空间取决于您正在处理的分区/文件系统。以下命令将打印当前文件夹中的可用磁盘空间:

TARGET_PATH="."
df -h "$TARGET_PATH"  | awk 'NR==2{print $4}'

TARGET_PATH是您要写入的文件夹。 df会自动检测该文件夹所属的文件系统。

答案 1 :(得分:1)

这是我能够获得可用磁盘空间量的唯一可移植方式(Linux和Mac OS):

df

请注意,来自Linux的#include <SPI.h> #include "PN532_SPI.h" #include "PN532.h" #include "NfcAdapter.h" PN532_SPI interface(SPI, 10); // create a PN532 SPI interface with the SPI CS terminal located at digital pin 10 NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object void setup(void) { Serial.begin(115200); // begin serial communication Serial.println("NDEF Reader"); nfc.begin(); // begin NFC communication } void loop(void) { Serial.println("\nScan an NFC tag\n"); if (nfc.tagPresent()) // Do an NFC scan to see if an NFC tag is present { NfcTag tag = nfc.read(); // read the NFC tag into an object, nfc.read() returns an NfcTag object. tag.print(); // prints the NFC tags type, UID, and NDEF message (if available) } delay(500); // wait half a second (500ms) before scanning again (you may increment or decrement the wait time) } 与来自Mac OS(OS X)的#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #define BITS_TO_EXPAND (4U) #define SIZE_MAX (256U) static bool expand_uint(unsigned int *toexpand,unsigned int *expanded); int main(void) { unsigned int in = 12; unsigned int out = 0; bool success; char buff[SIZE_MAX]; success = expand_uint(&in,&out); if(false == success) { (void) puts("Error: expand_uint failed"); return EXIT_FAILURE; } (void) snprintf(buff, (size_t) SIZE_MAX,"%u expanded is %u\n",in,out); (void) fputs(buff,stdout); return EXIT_SUCCESS; } /* ** It expands an unsigned int so that every bit in a nibble is copied twice ** in the resultant number. It returns true on success, false otherwise. */ static bool expand_uint(unsigned int *toexpand,unsigned int *expanded) { unsigned int i; unsigned int shifts = 0; unsigned int mask; if(NULL == toexpand || NULL == expanded) { return false; } *expanded = 0; for(i = 0; i < BIT_TO_EXPAND; i++) { mask = (*toexpand >> i) & 1; *expanded |= (mask << shifts); ++shifts; *expanded |= (mask << shifts); ++shifts; } return true; } 不同,并且它们只共享有限数量的选项。

这将返回千字节中的可用磁盘空间量。不要尝试使用不同的度量,因为它们的选项不可移植。

答案 2 :(得分:0)

#!/bin/bash
Check_space() { 
    set -e
    cd
    Home=$PWD
    reqSpace=100000000
    SPACE= df "$Home"
    if [[ $SPACE -le reqSpace ]]
    then
    $SPACE
    echo "Free space on  " 
    fi
}
Check_space