# Bash to search file for string and lauch function if found

**URL:** <https://community.unix.com/t/bash-to-search-file-for-string-and-lauch-function-if-found/366754>\
**Category:** Shell Programming and Scripting\
**Created:** [May 25, 2017, 11:50am UTC](https://community.unix.com/t/bash-to-search-file-for-string-and-lauch-function-if-found/366754 "2017-05-25T11:50:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmccabe](https://community.unix.com/letter_avatar/cmccabe/32/6_449c533f586939b6e5f246632a46421f.png) [@cmccabe](https://community.unix.com/u/cmccabe)\
**Post date:** [May 25, 2017, 11:50am UTC](https://community.unix.com/t/bash-to-search-file-for-string-and-lauch-function-if-found/366754/1 "2017-05-25T11:50:54Z")

</div>

In the `bash` below I am searching the file `virus-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 :).

virus-scan.log

```nohighlight
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)

```

```nohighlight
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

```nohighlight
execute() {
...
...
...
additional
}

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [May 25, 2017, 4:39pm UTC](https://community.unix.com/t/bash-to-search-file-for-string-and-lauch-function-if-found/366754/2 "2017-05-25T16:39:43Z")

</div>

How about this:

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![cmccabe](https://community.unix.com/letter_avatar/cmccabe/32/6_449c533f586939b6e5f246632a46421f.png) [@cmccabe](https://community.unix.com/u/cmccabe)\
**Post date:** [May 27, 2017, 11:19am UTC](https://community.unix.com/t/bash-to-search-file-for-string-and-lauch-function-if-found/366754/3 "2017-05-27T11:19:43Z")

</div>

Thank you very much, works perfect :).
