Re: home server for email box
On Sun, Mar 12, 2023 at 12:13:54PM +0100, Yassine Chaouche wrote:
> function net.ip.reputation(){
> revip=$(net.ip.reverse "$1")
> results=$(dig +short $revip.zen.spamhaus.org)
> [[ -z $results ]] && (echo "clean"; return 0)
You're caling return inside a subshell. This doesn't actually return
from the function, since it occurs in a child process.
unicorn:~$ f() { true && (echo true; return 0); echo problem; }
unicorn:~$ f
true
problem
You either need to use "if", or use a curly-braced command group instead
of your parenthesized subshell command.
[[ -z $results ]] && { echo "clean"; return 0; }
It would also be polite to declare your function's local variables as
"local".
> for result in $results
You're using 3 variables locally, so you could add
local revip results result
at the top of the function.
Finally, you're relying on the output of "dig +short ..." to be safely
word-splittable. I don't know whether this is always going to be true.
However, it does look like dig +short writes its output as a sequence
of items, one per line. This means you can read them into an array
without performing word-splitting or globbing:
mapfile -t results < <(dig +short "$revip".zen.spamhaus.org)
if (( ${#results[@]} == 0 )); then
echo clean
return 0
fi
for result in "${results[@]}"
do
...
The way you've got it *might* be OK. I just don't know whether the
output of dig +short can ever contain internal whitespace or globbing
characters. I'd rather not take that chance.
Reply to: