Playing with bash...

Hi,

this script

#!/bin/bash
OGGI=$(date '+%b %e')
MAILLOG="/var/log/maillog"
SENT="status=sent"
GREPOGGI=$(grep $OGGI $MAILLOG | grep $SENT | grep -c $SENT)

echo $GREPOGGI

is supposed to count the sent email in the maillog file.

it does it very well but when I execute it says:

grep: 14: No such file or directory
27

where 27 is the number of the sent emails.

Why it works but than it says no such file?

2 Replies

Scripting 101

You need "" around $OGGI in the grep because OGGI has a space in it.

GREPOGGI=$(grep "$OGGI" …)

However, grep | grep | grep is… umm.

Can't you just do

GREPOGGI=$(grep -c "^$OGGI .* $SENT")

@sweh:

Scripting 101

You need "" around $OGGI in the grep because OGGI has a space in it.

GREPOGGI=$(grep "$OGGI" …)

However, grep | grep | grep is… umm.

Can't you just do

GREPOGGI=$(grep -c "^$OGGI .* $SENT")

thank you sweh.

I noticed that grepping on

SENT="smtp.*to=.*sent.*"

gives me a more "reliable" number.

I don't care about the email sent from dovecot, dovecot carry emails around my system internally, I don't want to count that.

I want to count only the email sent to the external and that grep helped me.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct