将 tail 与 if 和 then 语句一起使用
Using tail with if and then statements
尝试(理论上)尾部日志文件的最后两行 (-n 2),然后使用 if/then 语句继续。这当然是一个将从 launchctl .plist 调用的脚本。
基本上我脑子里是这样的,虽然不对...
#!/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
如果你想做的是测试文件包含 正好 两行,那么你只想使用 wc -l
并在标准输入上输入内容(以避免 wc
像往常一样打印出文件名)。
#!/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.
通过标准输入输入 wc -l
文件在这里很重要,因为正常调用时(即 wc -l filename
)wc
“有帮助”打印行数 和 标准输出的文件名,因此需要字段 splitting/etc。以获得适合比较的数字。当 wc
正在读取标准输入时,它没有文件名,因此不会这样做。
注:
只有当您不关心内容时,这种做法才安全wc
完成执行后的文件。
如果您在脚本的其余部分使用 文件的内容,那么这是一个典型的 Time-of-Check Time-of-Use 漏洞。有关此主题的更多信息,请参阅 this page on MITRE's website and this Wikipedia entry。
尝试(理论上)尾部日志文件的最后两行 (-n 2),然后使用 if/then 语句继续。这当然是一个将从 launchctl .plist 调用的脚本。
基本上我脑子里是这样的,虽然不对...
#!/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
如果你想做的是测试文件包含 正好 两行,那么你只想使用 wc -l
并在标准输入上输入内容(以避免 wc
像往常一样打印出文件名)。
#!/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.
通过标准输入输入 wc -l
文件在这里很重要,因为正常调用时(即 wc -l filename
)wc
“有帮助”打印行数 和 标准输出的文件名,因此需要字段 splitting/etc。以获得适合比较的数字。当 wc
正在读取标准输入时,它没有文件名,因此不会这样做。
注:
只有当您不关心内容时,这种做法才安全wc
完成执行后的文件。
如果您在脚本的其余部分使用 文件的内容,那么这是一个典型的 Time-of-Check Time-of-Use 漏洞。有关此主题的更多信息,请参阅 this page on MITRE's website and this Wikipedia entry。