Bigboss Bigboss - 1 month ago 8
Bash Question

Shell conditions inside if statement

I am looking to create a monitor and need some help on the below piece of code. I am having error in if statement now. I need the look to check last parameter of each line of file and compare it with a 85 and echo meaning full output.

while read line;
do
if [$(cat $line | awk -F':' '{print $5}') -gt 85]
echo "Rootfile system on host $1 has reached $5 percentage"
fi
echo $line
done </home/monitor1.txt


My file output looks as below:

host1:45G:10G:35G:34
host2:45G:15G:30G:41
host3:45G:5G:40G:12
host4:45G:5G:40G:11

Answer

You need space(s) after [ command and before the closing argument ], and also a then after ]:

if [ "$(awk -F':' '{print $5}' <<<"$line")" -gt 85 ]; then
    echo ...
fi

Or

if [ "$(awk -F':' '{print $5}' <<<"$line")" -gt 85 ]
then
    echo ...
fi

Also you are reading a string, not a file so cat is being used wrongly here, either use echo or here string. Also quote your variable expansions to avoid word splitting, and pathname expansion. Also you can incorporate the whole logic inside awk, no need for shell's help.