BASH- Need help pulling data from .emlx

Hello, fellow computer junkies. First time poster! My boss wrote an application (Mavericks 10.9, Mountain Lion 10.8) that checks a user's security settings. The user runs the application, then it spits out an email that is sent back to our inbox showing the results. On our end, we have a mail rule setup which sorts all of those results messages into a local mailbox. The email body looks something like this:

Operating System- 10.9
HDD Used- 79%
Filevault Enabled- PASS
Firewall Enabled- FAIL
VPN split tunneling disabled- PASS

I am writing a script that will help me manage these emails. The goal is to run the script and have it spit out who has passed and who has failed into a simple spreadsheet. The identifiers can be the PASS or FAIL status in the message. Basically, if it says FAIL in the message I want it sorted to the "Fail" Column on the spreadsheet. If it does not contain FAIL, it can sort to the Pass column of the spreadsheet

I've STARTED with the code below but it does't work on files that have spaces in the name. It does, however, work on files with no spaces in the name. My train of thought is sort the messages between pass and fail, then output the email address to a spreadsheet appropriately. Anyways, any help is greatly appreciated! Assume I am a N00B :b:

find pathname -type f | zargs grep -li fail

You could try this:

find pathname -type f | while read filename
do
    if grep -qi fail "$filename"
    then
         printf "$filename\tFAIL\t\n"
    else
         printf "$filename\t\tOK\n"
    fi
done > outfile.txt
1 Like

This helps me with my first step, thank you. Can you explain the characters used?

 "$filename\tFAIL\t\n"
         "$filename\t\tOK\n"

I'm trying to make sense of the "t" and "OK" used. I understand the rest.

\t is a TAB character \n is a New Line, so we are creating a file for import into a sheet with 3 TAB separated columns filename,fail and pass. Probably should have used PASS instead of OK as this is what you mentioned in your first post.

1 Like

Got it, so I should change the OK to PASS. I appreciate the explanation, now lets see if I can finish this off.

small improvements. not that it's likely you'll have % in your filenames, but you should use printf properly: printf '%s\tFAIL\t\n' "$filename"

if using bash you'd also want read -r to disable backslash processing too.

examples:

mute@thedoctor:~$ read var <<< 'file\name'; echo "$var"
filename
mute@thedoctor:~$ read -r var <<< 'file\name'; echo "$var"
file\name
mute@thedoctor:~$ filename='%strange'; printf "$filename\tOK\n"
trange  OK
2 Likes