Home > Software design >  Bash Script If else statement not capturing the threshold
Bash Script If else statement not capturing the threshold

Time:05-25

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. 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

CodePudding user response:

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.

CodePudding user response:

Using --threshold option

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
  • Related