-2

我想创建一个 bash 脚本,该脚本将从文本文件中获取条目并根据要求对其进行处理。

我试图找出每个邮件帐户的最后登录时间。我有一个文本文件 email.txt 逐行包含所有电子邮件地址,需要使用以下命令检查每个帐户的最后登录时间:

cat /var/log/maillog | grep 'mail1@address.com' | grep 'Login' | tail -1 > result.txt

所以结果会在 result.txt 里面

谢谢,

4

3 回答 3

2

I think you are looking for something like this.

while read email_id ; do grep "$email_id" /var/log/maillog   | grep 'Login' >> result.txt ; done < email.txt
于 2013-10-30T11:33:00.587 回答
0

并行获取所有电子邮件地址(而不是重复扫描可能非常大的日志文件)。

grep -f  file_with_email_addrs /var/log/maillog

这是否值得取决于日志文件的大小和您获得的电子邮件地址的数量。一个小日志文件或少数用户可能会呈现此静音。但是一个大的日志文件和很多用户将使这是一个很好的第一步。但随后将需要一些额外的处理。

我不确定日志行的格式或文件中登录名的相对分布,所以接下来会发生什么取决于更多信息。

# If logins are rare?
grep Login /var/log/maillog | grep -f file_with_email_addrs | sort -u -k <column of email>

# If user count is small
grep -f file_with_email_addrs /var/log/maillog | awk '/Login/ {S[$<column of email>] = $0;} END {for (loop in S) {printf("%s\n",S[loop]);}'
于 2013-10-30T12:15:23.513 回答
-1

Your question is unclear, do you want to look for the last login for 'mail_address1' which is read from a file?

If so:

#/bin/bash
while read mail_address1;
do;
# Your command line here
cat /var/log/maillog | grep $mail_address1 | grep 'Login' | tail -1 >> result.txt
done; < file_with_email_adddrs

PS: I've changed the answer to reflect the suggestions (extremely valid ones) in the comments that advise against using a for i in $(cat file) style loop to loop through a file.

于 2013-10-30T11:31:44.870 回答