12 Replies Latest reply on Mar 22, 2018 12:44 PM by Robeen

    check last line in file

    Robeen

      Red Hat Linux 6

       

      Hi,

       

      I want to write a bash script to to check if last line in a file is equal to  "Import dump completed successfully".

      The condtion is if "Import dump completed successfully" is not present in logfile, error will be written to a variable in another file(output.properties)

       

      cd /oldbackup/DNRTOOL/cidev/exportlog

      arch1=$(ls -t | head -1)

       

      cd /oldbackup/DNRTOOL/gaiadev/exportlog

      arch2=$(ls -t | head -1)

       

      cd /oldbackup/DNRTOOL/gcidev/exportlog

      arch3=$(ls -t | head -1)

       

      if [ $(\grep "IMP-00000: Import terminated successfully" $arch1 ] <--- to check

       

       

              then

              sed -i 's/^IMPORTGAIAGCICIFLAG.*/IMPORTGAIAGCICIFLAG=ERROR IN CI/g'                /comptel/output.properties

       

       

      if [ $(\grep "IMP-00000: Import terminated successfully" $arch2 ]  <--- to check

       

       

              then

              sed -i 's/^IMPORTGAIAGCICIFLAG.*/IMPORTGAIAGCICIFLAG=ERROR IN GAIA/g'            /comptel/output.properties

       

       

      if [ $(\grep "IMP-00000: Import terminated successfully" $arch3 ] <--- to check

              then

              sed -i 's/^IMPORTGAIAGCICIFLAG.*/IMPORTGAIAGCICIFLAG=ERROR IN GCI/g'             /comptel/output.properties

       

       

       

       

      fi

      fi

      fi

       

      Can anyone please guide me for the if statement, if "IMP-00000: Import terminated successfully" is not present in $arch1 or $arch2 or $arch3, then output the ERROR  message in /comptel/output.properties

       

      Thanks,

       

      Joe

        • 1. Re: check last line in file
          Nik

          Hi.

          For example:

           

             res=`grep "IMP-00000: Import terminated successfully"  $arch1  $arch2 $arch3`

             if [ "$res= "" ] ; then  echo "Error" > /comptel/output.properties

             fi

           

          Regards,

            Nik

          1 person found this helpful
          • 2. Re: check last line in file
            Igoroshka

            1. Are you sure "Import dump completed successfully" is the last line? Are there other rows/empty rows possible?

            2. What about to have multiple "Import dump completed successfully" rows in the file?

            3. What do you mean "error will be written to a variable in another file"?

            • 3. Re: check last line in file
              handat

              Assuming you want an error message 'Error Occured' in a variable called ERROR, then the following could do it:

               

              res=`grep "IMP-00000: Import terminated successfully"  $arch1  $arch2 $arch3`

                 if [ "$res" = "" ] ; then  echo "\$ERROR='Error Occured'" >> /comptel/output.properties

                 fi

              1 person found this helpful
              • 4. Re: check last line in file
                Robeen

                1. Not in the last line. It should be present in the file

                 

                3. the work ERROR will be appended (using echo) to a variable in a file.

                • 5. Re: check last line in file
                  Robeen

                  I am getting

                   

                  oracle@rh-ftthprod-db1 DNRTOOL]$ sh checkERR2.sh

                  importgaia090320180922.log

                  checkERR2.sh: line 18: unexpected EOF while looking for matching `"'

                  checkERR2.sh: line 20: syntax error: unexpected end of file

                  • 6. Re: check last line in file
                    handat

                    Check for single " character. There should always be a pair of them around a string.

                    • 7. Re: check last line in file
                      Dude!

                      How about the following:

                       

                      #!/bin/bash

                       

                      out=/comptel/output.properties

                      log1=$(ls -1t /oldbackup/DNRTOOL/cidev/exportlog | head -1)

                      log2=$(ls -1t /oldbackup/DNRTOOL/gaiadev/exportlog | head -1)

                      log3=$(ls -1t /oldbackup/DNRTOOL/gcidev/exportlog | head -1)

                       

                      lastlog() {

                        local status

                        tail -n 1 $1 | grep 'MP-00000: Import terminated successfully' >/dev/null 2>&1

                        status=${PIPESTATUS[@]}

                        [ "$status" != "0 0" ] && echo $1 was not successfull >> $out

                      }

                       

                      lastlog $log1

                      lastlog $log2

                      lastlog $log3

                      • 8. Re: check last line in file
                        Dude!

                        1. Not in the last line. It should be present in the file

                         

                        That's not what you wrote in your initial post, but that simplifies it:

                         

                         

                        #!/bin/bash

                         

                        out=/comptel/output.properties

                        log1=$(ls -1t /oldbackup/DNRTOOL/cidev/exportlog | head -1)

                        log2=$(ls -1t /oldbackup/DNRTOOL/gaiadev/exportlog | head -1)

                        log3=$(ls -1t /oldbackup/DNRTOOL/gcidev/exportlog | head -1)

                         

                        lastlog() {

                          grep 'MP-00000: Import terminated successfully' $1 >/dev/null 2>&1 \

                            || echo "$1 was not successfull" >> $out

                        }


                        lastlog $log1

                        lastlog $log2

                        lastlog $log3

                        • 9. Re: check last line in file
                          Robeen

                          Hi,

                           

                          I have tested the script for one of my import dump logfile.

                           

                           

                          content of script:

                          out=/comptel/DNR/DNRTOOL/FILES/application.properties

                          log2=$(ls -1t /oldbackup/DNRTOOL/gaiadnr/exportlog | head -1)

                          lastlog() {

                            grep 'Import terminated successfully with warnings.' $1 >/dev/null 2>&1 \  || sed -i 's/^IMPORTGAIAGCICIFLAG.*/IMPORTGAIAGCICIFLAG=ERROR/g' $out

                          }

                          lastlog $log2

                           

                          I see I am still getting 'ERROR' for flag IMPORTGAIAGCICIFLAG

                           

                          Please advise.

                           

                          Regards

                          Joe

                          • 10. Re: check last line in file
                            Nik

                            Hi.

                            You lost line

                            || echo "$1 was not successfull" >> $out

                             

                            Return this line to your script.

                             

                            Regards,

                            Nik

                            • 11. Re: check last line in file
                              EdStevens

                              Robeen wrote:

                               

                              Hi,

                               

                              I have tested the script for one of my import dump logfile.

                               

                               

                               

                               

                              How many times have you been asked to NOT post screen shots of terminal sessions?

                              • 12. Re: check last line in file
                                Robeen

                                ok noted