Error in if statement

I am working on script for stale nfs.

the file consists of

cat data01stale.log
-               -               -               -               /abcd/backup
-               -               -               -               /abcd/data



Script

#!/bin/bash
set -x
#
grep -  data01awk.log

if [ $? -eq 0 ]; then



              if [ `data01stale.log | wc -l` -eq 1 ]; then
              awk '$1 $2 $3 == "---" { exit 1 }' data01stale.log
              #awk '$2 $3 $4 == "---" { exit 1 }' data01stale.log}

                     if [ $? -ne 0 ]; then
                      echo "Found NFS issues on data01"
                      u=$(awk '{ print $5 }' data01stale.log)
                     
                      printf "                 NFS issues on data01                                                \n"
                     
                      #grep -i $u data01Out.log
                      echo -e "[root@data01 ]# df -hP | grep -i $u \n"
                      #df -hP | grep -i $u
                      echo " "
                     fi

else

              echo "Found Multiple NFS issues on data01"
              for u in `(data01stale.log |  awk '{print $5}')`
              do
              
                  echo -e "[root@data01 ]# df -hP | grep -i $u \n"
                      df -hP | grep -i $u
                      echo " "
                     fi
else
printf "                      NO  NFS issues on ausomxdta01                                       \n"
echo -e "[root@data01 ${UUID}]# df -hP \n"
df -hP
exit
fi

done

Please give the error you're seeing in console. It will be useful to understand the issue rather reading ur script code.

line 40: syntax error near unexpected token `fi'
line 40: `fi '

check the if...else...fi loop.
U had written
fi and then else part of code.

A proper indentation and syntax coloration helps a lot#!/bin/bash
set -x
#
grep - data01awk.log
if [ $? -eq 0 ]
then
if [ `data01stale.log | wc -l` -eq 1 ]
then
awk '$1 $2 $3 == "---" { exit 1 }' data01stale.log
#awk '$2 $3 $4 == "---" { exit 1 }' data01stale.log}
if [ $? -ne 0 ]
then
echo "Found NFS issues on data01"
u=$(awk '{ print $5 }' data01stale.log)
printf " NFS issues on data01 \n"
#grep -i $u data01Out.log
echo -e "[root@data01 ]# df -hP | grep -i $u \n"
#df -hP | grep -i $u
echo " "
fi
else
echo "Found Multiple NFS issues on data01"
for u in `(data01stale.log | awk '{print $5}')`
do
echo -e "[root@data01 ]# df -hP | grep -i $u \n"
df -hP | grep -i $u
echo " "
fi
else
printf " NO NFS issues on ausomxdta01 \n"
echo -e "[root@data01 ${UUID}]# df -hP \n"
df -hP
exit
fi
done

Line 28 You have an unexpected fi statement before the end of for .. in .. do .. done

Hi
Could some one please check and help me what could be the correct syntax

the last part of code should be as follows,

else
echo "Found Multiple NFS issues on data01"
for u in `(data01stale.log | awk '{print $5}')`
do
echo -e "[root@data01 ]# df -hP | grep -i $u \n"
df -hP | grep -i $u
echo " "
done
fi
else
printf " NO NFS issues on ausomxdta01 \n"
echo -e "[root@data01 ${UUID}]# df -hP \n"
df -hP
exit
fi

Thanks a lot for all .Its working Fine Now

I am getting below error with extension of above script.

#!/bin/bash
#set -x
#
grep -  hostawk.log

if [ $? -eq 0 ]; then



              if [ `cat hoststale.log | wc -l` -eq 1 ]; then
              awk '$1 $2 $3 == "---" { exit 1 }' hoststale.log
              #awk '$2 $3 $4 == "---" { exit 1 }' hoststale.log}

                     if [ $? -ne 0 ]; then
                      echo "Found NFS issues on host"
                      u=$(awk '{ print $5 }' hoststale.log)
                      printf "======================================================================================\n"
                      printf "                 NFS issues on host                                                \n"
                      printf "======================================================================================\n"
                      #grep -i $u hostOut.log
                      echo -e "[host@host ]# df -hP | grep -i $u \n"
                      #df -hP | grep -i $u
                      echo " "
                     fi

else
              echo " "
             ## if [ `hoststale.log | wc -l` -gt 1 ]; then
              printf "======================================================================================\n"
              printf "               Found Multiple NFS issues on host                               \n"
              printf "======================================================================================\n"
              echo " "
              printf "======================================================================================\n"
              printf "                 NFS issues on host                                                \n"
              printf "======================================================================================\n"

              for u in `(cat hoststale.log |  awk '{print $5}')`
              do
              #printf "======================================================================================\n"
              #printf "                 NFS issues on host                                                \n"
              #printf "======================================================================================\n"
              #grep -i $u hostOut.log
              #echo -e "[host@host ]# df -hP | grep -i $u \n"
              #printf "======================================================================================\n"
              #printf "                 NFS issues on host                                                \n"
              #printf "======================================================================================\n"
              grep -i $u hostOut.log
              #df -hP | grep -i $u
              echo " "
done


fi
#elif [ -f hostawk.log ]; then
elif [ $#  [ `(cat hostawk.log | grep 1 | egrep -v "G|T|K|M")` ]  -eq 0 ]; then
cat hostawk.log | grep 1 | egrep -v "G|T|K|M"
if [ $? -eq 0 ]; then
     #cat hostawk.log | grep 1 | egrep -v "G|T|K|M" >>hoststale1.log
     #chmod 777 hoststale.log
     awk '$1 $2 $3 == "1" { exit 1 }' hoststale.log
     if [ $? -eq 0 ]; then
     echo "Found NFS issues with 1 on host"
     for u in `(cat hoststale.log |  awk '{print $5}')`
     do
     #grep -i $u hostOut.log  >>/hostfinal.log
     echo $u
     #chmod 777 hostfinal.log
     #mountstale
     done
     fi
fi
else
printf "======================================================================================\n"
printf "                      NO  NFS issues on host                                       \n"
printf "======================================================================================\n"
#echo  `cat  hostOut.log`
echo -e "[host@host ${UUID}]# df -hP \n"
#df -hP
exit
fi


This is the file where its checking
================================

[host@host1 host]# cat hostawk.log
8.9G            2.5G            6.0G            29%             /
28G             25G             3.5G            88%             /dtestx
57G             49G             8.4G            86%             /ttestx
16G             2.4G            13G             16%             /dtesti/out
1               1               1               0%              /ttesti/out
[host@host1 host]#
 

Error I am getting
=================

+ '[' 0 '[' 1 1 1 0% /tbmidi/out ']' -eq 0 ']'
line 55: [: too many arguments


Line number 55
----------------------
 55  elif [ $#  [ `(cat hostawk.log | grep 1 | egrep -v "G|T|K|M")` ]  -eq 0 ]; then


Please help how to check this argument.

Hi, line 55 is just invalid syntax.

Hi
Could you please help me how to correct it.

What are you trying to test?

let me clearly explain you.I am checking for Stale NFS issues on Host.

This is one example of one host for Stale NFS issue 


type1
==========
[root@host2 host3]# cat host3awk.log
8.9G            6.3G            2.2G            75%             /

79G             5.5G            74G             7%              /txxxxi/mt
-               -               -               -               /txxxxi/out
117G            44G             74G             38%             /txxxxi/app
78G             4.7G            74G             6%              /txxxxi/prod
74G             458M            74G             1%              /txxxxi/in
[root@host2 host3]#

type2
============


[host@host1 host]# cat hostawk.log
8.9G            2.5G            6.0G            29%             /
28G             25G             3.5G            88%             /dtestx
57G             49G             8.4G            86%             /ttestx
16G             2.4G            13G             16%             /dtesti/out
1               1               1               0%              /ttesti/out
[host@host1 host]#
 
Sometimes there are mutiple nfs  issues.

For no NFS issue the output of file is this 
------------------------------------------

[root@host1 host4]# cat host4awk.log
8.9G            2.2G            6.3G            26%             /
29G             9.0G            20G             32%             /taaaa/app
32G             13G             20G             40%             /taaaa/di
[root@host1 host4]#

I have wriiten script to check the NFS issue of this type

OK, but what are you trying to test on line 55?

I want to check this in single command with elif

cat hostawk.log | grep 1 | egrep -v "G|T|K|M"
if [ $? -eq 0 ]; then


I tried using this 

elif [ $#  [ `(cat hostawk.log | grep 1 | egrep -v "G|T|K|M")` ]  -eq 0 ]; then

try:

if grep 1 hostawk.log | egrep -qv "G|T|K|M"; then
1 Like

Thanks its worked