Trying (In theory) to tail a log file, of the last two lines (-n 2), then proceed using if/then statements. This of course, would be a script that would be called from the launchctl .plist.
Basically it looks like this in my head, although it's not right...
#!/bin/sh
last-entry= tail -n 2 command # show only last two lines of log file
if last-entry is less than (<) two lines, then
execute command here
fi
If what you are trying to do is test that the file contains exactly two lines then you just want to use wc -l
and feed it the contents on standard input (to avoid wc
printing out the filename as it normally does).
#!/bin/sh
if [ "$(wc -l < /private/var/log/accountpolicy.log)" -ne 2 ]; then
exit
fi
# Do whatever you want when it does contain exactly two lines here.
Feeding wc -l
the file via standard input is important here because when called normally (i.e. wc -l filename
) wc
"helpfully" prints the line count and the filename to standard output and thus requires field splitting/etc. to obtain a number suitable for comparison. When wc
is reading standard input it doesn't have a filename and so doesn't do this.
This practice is only safe when you do not care about the actual contents of the file once wc
has finished executing.
If you use the contents of the file in the rest of the script then this is a classic Time-of-Check Time-of-Use vulnerability. See this page on MITRE's website and this Wikipedia entry for more on this topic.