problem in -->df command monitoring script

Hi All,

I am creating df monitoring script. but facing some below problm.

Please see the below code.

 
df_threshold=90;
for i in `cat df_fnl.txt`
do
if [ $(echo "$i > $df_threshold"|bc) -eq 1 ] ; then
echo "%df is more than $df_threshold "
fi
done

and df_fnl.txt contains the data....

 
50%
5%
Use%
1%
11%
 

but it giving me the below error-

./df.sh: line 17: [: -eq: unary operator expected
(standard_in) 1: syntax error
./df.sh: line 17: [: -eq: unary operator expected
(standard_in) 1: illegal character: U
(standard_in) 1: syntax error
./df.sh: line 17: [: -eq: unary operator expected
(standard_in) 1: syntax error
./df.sh: line 17: [: -eq: unary operator expected
(standard_in) 1: syntax error
./df.sh: line 17: [: -eq: unary operator expected

Please help me to solve the above error .

and how can i remove % symbol from df_fnl.txt file.

so that it will be easier me to compair the values.

Thanks

#! /bin/bash
df_threshold=90;
while read x
do
    x=${x%\%}
    echo $x | egrep -q "^[0-9]*$"
    [ $? -ne 0 ] && continue
    if [ $(echo "$x > $df_threshold"|bc) -eq 1 ] ; then
        echo "$x is more than $df_threshold "
    fi
done < df_fnl.txt
1 Like

Hello Aish11 ,

In order to remove the % symbol in your sample input file use

for i in `cat df_fnl.txt |  sed 's/%//g'`

Does the below code segment is what you need ?

df_threshold=90;
for i in `cat df_fnl.txt |  sed 's/%//g'`
do
if [[ $i -gt $df_threshold ]] ; then
echo "%df is more than $df_threshold "
fi
done
1 Like

Use "-gt" insted of symbol ">" .

--Shirish

Thanks balajesuri and codemaniac .

your code is working.

---------- Post updated at 04:43 AM ---------- Previous update was at 04:40 AM ----------

Hi balajesuri,

can u plz explain me the below code?? how it removes the % symbol

x=${x%\%}
    echo $x | egrep -q "^[0-9]*$"

 
x=${x%\%}

Read about the bash string manipulation.

Manipulating Strings

@aish11: ${x%\%} --> Removes the shortest match of a pattern from the end of string. So, in this case, it finds for % (which is escaped) from the end and removes it.

echo $x | egrep -q "^[0-9]*$" --> This line checks if the line being read contains only a number after removing %. I saw an entry in df_fnl.txt is 'Use%', which doesn't form a valid number after removing % symbol and hence this check.

And as codemaniac and Shirishlnx suggested, use -gt rather than using > and invoking bc.