Bash to search file for string and lauch function if found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to search file for string and lauch function if found
# 1  
Old 05-25-2017
Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK.
If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the
file is sent to the corrupt folder. The amount of lines in the file can vary, in this example there are 6, but next time 9 lines. The format is always the same (output from clamscan). Currently, I start the process with a while loop, that does work but I am trying to automate it. Thank you Smilie.

virus-scan.log
Code:
Wed May 17 12:46:26 CDT 2017
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_000_00-0000_Last-First_R_2017_00_00_00_00_00_user_S5-00000-00-run.bam: OK
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_000_00-0000_Last-First_R_2017_00_00_00_00_00_user_S5-00000-00-run.bam.bai: OK
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_000_00-0000_Last-First_R_2017_00_00_00_00_00_user_S5-00000-00-run.vcf: OK
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_001_11-1111_La-Fi_R_2017_00_00_00_00_00_user_S5-00000-00-run.bam: OK
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_001_11-1111_La-Fi_R_2017_00_00_00_00_00_user_S5-00000-00-run.bam.bai: OK
/home/cmccabe/Desktop/NGS/API/R_2017_00_00_00_00_00_user_S5-00000-00-run/IonXpress_001_11-1111_La-Fi_R_2017_00_00_00_00_00_user_S5-00000-00-run.vcf: OK

 ----------- SCAN SUMMARY -----------
Known viruses: 6284751
Engine version: 0.99.2
Scanned directories: 2
Scanned files: 6
Infected files: 0
Data scanned: 93.80 MB
Data read: 140872.99 MB (ratio 0.00:1)
Time: 15.473 sec (0 m 15 s)

Code:
logfile="/home/cmccabe/Desktop/NGS/API/$filename/process.log"   # define log
file="/home/cmccabe/virus-scan.log"   # file to search
echo "Start folder verification $(date) - File: $file"    # write header line to process log
    if grep -iq "(Infected files: 0  && OK)" "${file}"; then   # look for Infected files: 0 at end on file and OK on each line
        echo "The folder is sucessfully verified." >> /home/cmccabe/medex.logs  # if found then write to analysis log
    else
        if [[ -f "$f" ]]; then   # if not found
               mv -f "$f" /home/cmccabe/Desktop/NGS/API/$filename /home/cmccabe/corrupt  # move specific line or lines in file to corrupt folder
            echo "The folder has a virus detected and has been moved to the folder at /home/cmccabe/Desktop/corrupt, please check log for reason."  >> /home/cmccabe/medex.logs/analysis.log # write to analysis log
        fi
    fi
    echo "End folder verification: $(date) - File: ${file}"  # add footer line to process log
done >> "$logfile"
 # Call function execute
     if [ "/home/cmccabe/medex.logs"="The folder is sucessfully verified" ]; then
        execute
     else
        echo "Nothing to do" && exit
fi

# function to call
Code:
execute() {
...
...
...
additional
}


Last edited by cmccabe; 05-25-2017 at 04:20 PM..
# 2  
Old 05-25-2017
How about this:

Code:
#!/bin/bash
logfile="/home/cmccabe/Desktop/NGS/API/$filename/process.log"   # define log
file="/home/cmccabe/virus-scan.log"   # file to search
echo "Start folder verification $(date) - File: $file" >> "$logfile"   # write header line to process log

sum=0
while read line
do
   ((++linenum == 1)) && continue
   [ -z "$line" ] && continue
   [[ $line = *SCAN\ SUMMARY* ]] && sum=1
   if ((sum == 0)) && [[ ${line} != *": OK" ]]
   then
      failed=1
      f=${line%:*}
      if [[ -f "$f" ]]
      then
         echo "The folder has a virus detected and has been moved to the folder at /home/cmccabe/Desktop/corrupt, please check log for reason."  >> /home/cmccabe/medex.logs/analysis.log # write to analysis log
         mv -f "$f" /home/cmccabe/Desktop/NGS/API/$filename /home/cmccabe/corrupt  # move specific line or lines in file to corrupt folder
      fi
   fi
   ((sum)) && [[ $line = "Infected files: "[1-9]* ]] && failed=1
done < $file

# add footer line to process log
echo "End folder verification: $(date) - File: ${file}" >> "$logfile"
if ((!failed))
then
  execute
else
    echo "Nothing to do" && exit
fi

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-27-2017
Thank you very much, works perfect Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command not found, but using function in bash

In the bash below, if the answer is "y" then goto function remove. If the answer is "n" then goto the id variable line (where the date is inputted). However, I am getting command remove not found, but remove is a function not an command. I must have the syntax incorrect? Thank you :). ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

4. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

5. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

6. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

7. Shell Programming and Scripting

bash script tail when string found do something

Okay, I have two scripts, the first one does some stuff, and comes to a point where it has this: Right here it runs a quick script to start something that writes to a log file. /usr/bin/tail -f ${pathVar}/nohup_${servVar}.out | while read -r line do ] && continue cd ${pathVar}... (0 Replies)
Discussion started by: cbo0485
0 Replies

8. Shell Programming and Scripting

bash search function

I want to have a function with a similar interface: search *.cpp asdf that will search recursively all directories for *.cpp file, containing line 'asdf' inside. I tried this: function search { find . -name "$1" | xargs grep -li $2; } But it doesn't work all the time. For example, if i run it... (3 Replies)
Discussion started by: doze
3 Replies

9. Shell Programming and Scripting

Search for string and display those NOT found

In my script I read a input file and search all the files in a directory and it's sub-directories for that string using: find . -type f -print | xargs grep $var1 This just displays all the lines the string was found on. Too much data. What I need is to store in a file one time those... (17 Replies)
Discussion started by: John Rihn
17 Replies

10. Shell Programming and Scripting

Recursive Search and replace only when found string

Hello all ( again ) I will like to search and replace string in text file ok I can loop throw the files like : foreach f ( ` find . -name "*."`) .. but here I like to examine the file if in contain the desired string and so do the sed -e 's/blah/foo/g' thingy on it or there is better way... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question