4 Replies Latest reply: Oct 8, 2013 2:20 AM by 446 RSS

    Nested if condition

    446


      have pasted part of my script which comprises nested if statement-

      if [ -n "$str1" ]

      then

      t=`strings $line |grep -i "\[INFO\] :"|head -1|cut -d":" -f2`

      t_count=`echo $t|wc -w`

      elif [-n "$str2" ]

      then

      t=`strings $line |grep -i "INFO: :"|cut -d ":" -f3,4|cut -d"." -f1`

      if [$(echo$t|grep -o ":"|wc -w) -eq 2]

      then

      t=`echo$t|sed -e 's/:/ hour /' -e 's/:/ minutes /' -e 's/$/ seconds/'`

      elif [$(echo$t|grep -o ":"|wc -w) -eq 1]

      t=`echo$t|sed -e 's/:/ minutes /' -e 's/$/ seconds/'`

      else    ----------------------------------------->

      t=`echo$t|sed -e 's/$/ seconds/'`

      fi

      elif [ -n "$str3" ]

      then

      t=`strings $line |grep -i "Total time:"|head -1|cut -d":" -f2`

      t_count=`echo $t|wc -w`

      else

      t_count=2

      t="0 second"

      fi

       

      am getting  line 5: syntax error at line 44: `else' unexpected (Else which is in nested if)

        • 1. Re: Nested if condition
          DK2010

          Hi,

          You are missing elif here try this

           

          if [ -n "$str1" ]

          then

          t=`strings $line |grep -i "\[INFO\] :"|head -1|cut -d":" -f2`

          t_count=`echo $t|wc -w`

          elif [-n "$str2" ]

          then

          t=`strings $line |grep -i "INFO: :"|cut -d ":" -f3,4|cut -d"." -f1`

           

           

          #--- Nested here so should be elif

           

           

          elif [$(echo$t|grep -o ":"|wc -w) -eq 2]

          then

          t=`echo$t|sed -e 's/:/ hour /' -e 's/:/ minutes /' -e 's/$/ seconds/'`

          elif [$(echo$t|grep -o ":"|wc -w) -eq 1]

          t=`echo$t|sed -e 's/:/ minutes /' -e 's/$/ seconds/'`

          else    ----------------------------------------->

          t=`echo$t|sed -e 's/$/ seconds/'`

          fi  # first if End here

           

           

          #--- You are Closing If Here so it would start with

           

           

          if [ -n "$str3" ]

          then

          t=`strings $line |grep -i "Total time:"|head -1|cut -d":" -f2`

          t_count=`echo $t|wc -w`

          else

          t_count=2

          t="0 second"

          fi  # secon if End here

           

          HTH

          • 2. Re: Nested if condition
            446

            Thanks...it worked out

            • 3. Re: Nested if condition
              Catch-22

              I think a case statement is often more readable and easier to use than if-elif constructs.

               

              Btw, what is reason for using the strings command? Are you dealing with binary content? $line looks like you are reading lines of a text file in do while or for loop.

              • 4. Re: Nested if condition
                446

                Yes .the file has binary content and $line is a text file