简体   繁体   English

Bash 脚本 If else 语句未捕获阈值

[英]Bash Script If else statement not capturing the threshold

I am currently attempting to capture the threshold in my script, but it's reading as everything is "above the threshold".我目前正在尝试在我的脚本中捕获阈值,但它正在读取,因为一切都“高于阈值”。

I know that the directory is at 48G and I set the threshold to 99G, expecting "Not above threshold" to appear after running the script.我知道目录是 48G,我将阈值设置为 99G,希望在运行脚本后出现“不高于阈值”。 Please advise and see script below for reference...请告知并查看下面的脚本以供参考...

#!/bin/bash

threshold=99

if [ "du -sh /data | cut -f1 | grep -Eo [0-9]+ -gt $threshold" ]
then
    echo "Reducing file with a further commands. File is above the threshold..."
else
    echo "Not above threshold..."
fi

You want:你要:

if [ "$(du -sh /data | cut -f1 | grep -Eo '[0-9]+')" -gt "$threshold" ]

Double quotes create a literal string, you need to use $(command) to substitute the output of a command into the command line.双引号创建一个文字字符串,您需要使用$(command)将命令的输出替换为命令行。

Using --threshold option使用--threshold选项

value=99
THRESHOLD=$(du -sh --threshold="${value}"G /data)

if [ -n "$THRESHOLD" ]
then
    echo "Reducing file with a further commands. File is above the threshold..."
else
    echo "Not above threshold..."
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM