开发者

How to loop in Bash until reaching free disk space limit?

开发者 https://www.devze.com 2023-02-05 07:04 出处:网络
I would like to do a set of operations (x y z) as long as I have at least 200MB free space on file-system mounted to /media/z.

I would like to do a set of operations (x y z) as long as I have at least 200MB free space on file-system mounted to /media/z.

How can I do that?

I tried something like

while (`df | grep /media/z | awk '{print $4}'` > 204800); do开发者_Python百科 x; y; z; done;

but I guess my while syntax is wrong.


( ) executes a command in a sub-shell. If you want to test a condition you have to use test command: [ ].

while [ `df | grep /media/z | awk '{print $4}'` -gt 204800 ]; do 
    x; y; z; sleep 5; 
done;


if you're using bash use [[ ]] to use the internal testing, it's faster and gives you standard operators:

while (( $(df /media/z | awk 'NR==2{print $4}') > 204800 )); do
    x; y; z
done
0

精彩评论

暂无评论...
验证码 换一张
取 消