0

Explanation: I'm playing a little bit with a script this one helps notifying to user about SSL certificate expiration. If certificate has less than 30 days to expires, this will notify to user, if isn't do nothing.

But for some weird reason validating with a greater constant, even the emails is sending, and this should not happening.

This is the code:

#!/bin/ksh
    legend="The next certificate almost expires.\n URL: url.com "
    count=30
    daysleft=$(./ssl-cert-check -d /etc/ssl/certs/cert.cer | awk '{print $6}' | tail -n 1);
    #daysleft=50 #Constant to do some test
    sendnot=$(echo $legend | mail -s "url.com SSL cert Expiration" [email protected] < <(./ssl-cert-check -d /etc/ssl/certs/cert.cer))
    if [[ $daysleft -lt $count ]];
    then
    echo "Sending note"
    echo $sendnot
    else
    echo "Doing nothing..."
    fi

Doing test with daysleft constant, the statement it works; but even this anyway is sending the notes to the user.

Edit: The output of:

./ssl-cert-check -d /etc/ssl/certs/cert.cer | awk '{print $6}' | tail -n 1

is 9

Any Questions or comments I will be ready to attending. Regards!!

6
  • 2
    you're running the mail command unconditionally, before the if-statement. In the command substitution in the assignment to sendnot.
    – ilkkachu
    Commented Sep 21, 2021 at 23:27
  • I thought it was only a constant declaration. Let me try it. Commented Sep 22, 2021 at 0:04
  • @ilkkachu you're right, I've moved command declaration into if statement. Thanks! such a dumb issue. Commented Sep 22, 2021 at 0:38
  • And when compare will be wise to add double quotes around the variables Commented Sep 22, 2021 at 5:41
  • 1
    If @ilkkachu doesn't post an actual answer, please feel free to answer the question yourself and mark it as accepted. That prevents it from popping up from the Unanswered queue in the future. Commented Sep 22, 2021 at 7:13

1 Answer 1

4

This line:

sendnot=$(echo ... | mail ... < <(./ssl-cert-check ...))

runs the commands echo, mail and ./ssl-cert-check right then and there, and captures the output of the whole lot, i.e. the output of mail, and assigns it in the variable sendnot. That's why you get the email sent regardless of the if-statement. Also, mail probably doesn't output much here, so using the command substitution seems needless.

If you want to just store a command for further use, make it a function.

send_notice() {
    local subject=$1
    echo "some message" | mail -s "$subject"
}
...
if whatever...; then
    send_notice "subject line..."
fi

($1 would be the first argument to the function.)

Also, there's another problem with that command: you're using both a pipe and an input redirection to mail. You can't connect two different things to the standard input of a command, so only one applies. With multiple redirections, it would be the last one that stays in effect; with a pipe and a redirection, it's the redirection.

So your command there is practically equal to:

mail -s ... < <(./ssl-cert-check ...)

i.e. the echo doesn't really do anything.

If you want to concatenate the outputs of the two commands, you have to do something like this:

(echo "some message"; ./ssl-cert-check ...) | mail ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .