[英]Execute a command which will check if the disk space on somepartion is greater than 1 KB, return -1 else return 0
Execute a command which will check if the disk space on somepartion is greater than 1 KB
, return -1
else return 0
执行一个命令,该命令将检查某分区上的磁盘空间是否大于
1 KB
,返回-1
否则返回0
For ex: 例如:
df| tail -n 1 | awk '{print $4}'
This command returns available space on root partition and this command returns 23% space on my disk. 该命令返回根分区上的可用空间,该命令返回磁盘上23%的空间。
I want it return -1
if the space is less than 1 KB
else return 0
如果空间小于
1 KB
我希望它返回-1
,否则返回0
and we can not write sh file for this so i want 0
and -1
answer through the command 而且我们不能为此写sh文件,所以我想通过命令
0
和-1
回答
UPDATED it should exit with that return code means it should exit with returncode 0 or -1 更新它应该以该返回码退出意味着它应该以返回码0或-1退出
This may do: 这可以做到:
df | awk 'END {print ($4<1024?"-1":"0")}'
0
You can change the number to any that fits your need. 您可以将数字更改为适合您的任何数字。
END
is used to get last line, instead of tail
END
用于获取最后一行,而不是tail
To get it into an exit/return code do: 要将其放入退出/返回代码,请执行以下操作:
(exit $(df | awk 'END {print ($4<1024?"-1":"0")}')); echo "$?"
PS exit -1
will give 255
PS
exit -1
将产生255
Use stat
to get the available blocks and the block size, create an expression and pipe into bc
. 使用
stat
获取可用块和块大小,创建一个表达式并将其传递到bc
。
Here I'm testing it with a value of 10240 bytes on a couple of FS, one with less than that free and one with more: 在这里,我在几个FS上使用10240 字节的值对其进行测试,其中一个FS的可用空间小于该可用空间,而另一个FS的可用空间大于该字节 :
$ stat -f /sys/fs/cgroup -c "(%a * %s >= 10240) - 1" | bc
-1
$ stat -f / -c "(%a * %s >= 10240) - 1" | bc
0
Adjust the 10240 to your wanted value in bytes . 将10240调整为所需的字节值。
Here's the conventional df
output: 这是常规的
df
输出:
$ df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 11440712 5517040 5319464 51% /
$ df /sys/fs/cgroup/
Filesystem 1K-blocks Used Available Use% Mounted on
none 4 0 4 0% /sys/fs/cgroup
The corresponding stat
being based on: 相应的
stat
基于:
$ stat -f / -c "%a * %s / 1024" | bc
5319464
Note that using this instead of awk
to parse the output of df
is less fragile to possible output format changes of df
. 需要注意的是使用这个,而不是
awk
的解析输出df
是可能的输出格式的变化不那么脆弱df
。
To set the exit code from a script, capturing the output looks ok: 要设置脚本的退出代码,捕获输出看起来不错:
#!/bin/bash
echo Testing $@
exit `stat -f $@ -c "(%a * %s >= 10240) - 1" | bc`
Note you get 255 and not -1 returned - but then -1 is out of range since exit codes can only be 0 to 255. 请注意,您得到255而不返回-1-但是-1超出范围,因为退出代码只能是0到255。
$ ./sizer.sh /sys/fs/cgroup/
Testing /sys/fs/cgroup/
$ echo $?
255
$ ./sizer.sh /
Testing /
$ echo $?
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.