0

When I try to match a string and do some conditions; it always fails to do so.

date=`date +%Y%m%d`
kol="/home/user/test_$date"
regex='Terminating the script'

if [ -f $kol ]; then

  sudo tail -f $kol | while read line; do 
     if [[  $line  = *"Terminating the"* ]]
     then

        echo "failed"

     else

        echo $line >> /home/user/test123_$date

     fi
else

   echo "File is not yet present"
   exit 0
fi 

I have also tried with regex and that to failed. So when ever I input the matching string into the file ($path) it wont output "failed"; Is there anything wrong in the code. Help is much appreciated.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • try putting then after ";" of if condition instead of next line and also add "" around $line , it should work fine – pritesh agrawal Jul 04 '17 at 03:32
  • 5
    What does "fails to do so" actually mean? Do you get an error message? Can you provide sample input that can be tested with your script? Note that `$path` is a reserved variable name. Consider testing your scripts at [shellcheck.net](http://www.shellcheck.net/) before posting, as well as doing your own syntax check with `bash -n yourscript.sh` – ghoti Jul 04 '17 at 03:39
  • @priteshagrawal i tried those changes but it didn't work. – Sajeesh Krishnan Jul 04 '17 at 04:20
  • @ghoti I didn't get any error messages but never the expected output too; there are no errors in syntax. – Sajeesh Krishnan Jul 04 '17 at 04:20
  • @SajeeshKrishnan , what is the content of your file , can you please share , the lines which matches the condition. – pritesh agrawal Jul 04 '17 at 04:26
  • Try adding `set -x` at the top of the script, and it'll print what it's doing as it runs. Add the output from when it tests the line that contains "Terminating the" to your question; it should clarify what's happening as the script runs. – Gordon Davisson Jul 04 '17 at 07:22
  • `path` is not reserved; `PATH` is (or rather, it already has a special meaning and should not be used for your own purposes). – chepner Jul 05 '17 at 13:54

1 Answers1

0

You can try this. At least it works for me:

sudo tail -f $1 | while read line; do

    if [[ $line = '' ]] 
    then
        break
    fi

    if [[  $line = *"Terminating the"* ]]
    then
        echo "failed"
    else
        echo $line >> /home/nurzhan/test123_$date
    fi
    done < "$1"

$path is parameter to the running script. Also note that by default the command tail returns only 10 last lines.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • it didnt work; it always echo's "failed" when i run with the changes. – Sajeesh Krishnan Jul 04 '17 at 04:21
  • @SajeeshKrishnan What doe you input file contain? I checked the script it works for me fine. – Nurjan Jul 04 '17 at 04:23
  • Its a log file created for the date and when then log fails logs "Terminating the" it should match the regex and output the echo if not write the $line to the other file. Thanks! – Sajeesh Krishnan Jul 04 '17 at 04:29
  • @SajeeshKrishnan Let's clarify, if the line contains "Terminating the" then it should go to the file test123_$date? Or it is the other way round? – Nurjan Jul 04 '17 at 04:32
  • it's the other way around; if the $line contains "Terminating the" it should echo "failed". – Sajeesh Krishnan Jul 04 '17 at 04:34
  • @SajeeshKrishnan, I checked my script on a simple input file and it worked as expected. Check yours more thoroughly. – Nurjan Jul 04 '17 at 04:36