0

So my web host adds a malware.txt file in my account if there is any. The file has a line with the path to file ending with a : for each line.

I am trying to use awk to print the path from this file and pipe the output into adding into an array. But it's not adding for some reason because when I print nothing happens. If I manually add an element to the array it prints.

Please help me understand how I am wrong.

#!/bin/bash

# Add list of infected files to an array
declare -A malware_listing=()
awk -F":" '/home/{print $1}' ~/malware.txt | while read file ; do malware_listing+=( ${file} ) ; done ;

# Print list of infected files
for i in "${malware_listing[@]}" ; do echo $i ; done ;
~                                                       
Phil Hanson
  • 109
  • 3
  • 10
  • 1
    Why do you need the array? Why not just `for i in $(awk ...); do ... ; done`? – Barmar May 14 '21 at 01:02
  • Cause I don't know what I'm doing for the most part but, that suggestion actually helps. I guess I was thinking of putting it in an array to do other things to it, but I don't have a lot to do. – Phil Hanson May 14 '21 at 01:09
  • If you do need an array so you can do multiple things with it, see the linked question for the general solution. – Barmar May 14 '21 at 01:11
  • What you gave let me do what I was trying to do, guess I didn't need an array at all, heh. Thanks! – Phil Hanson May 14 '21 at 01:29
  • Although it's unsafe if the output of `awk` could contain spaces. – Barmar May 14 '21 at 01:32

0 Answers0