HTF
HTF

Reputation: 7280

BASH: append loop output to an email

I want the output of this script to be a body of the email message but I don't want to redirect it to a file first and then to an email, basically no external login/output files - all action should be done within the script itself - is it possible to do it?

Example:

#!/bin/bash

email() {

        echo "Results:"

}

for i in $(ls -1 test); do

if [ -f "test/$i" ]; then
        echo "'$i' it's a file."
else

        echo "'$i' it's a directory."
fi

done

email | mail -s "Test" [email protected]

Output:

$ ./tmp.sh 
'd1' it's a directory.
'f1' it's a file.

Upvotes: 0

Views: 187

Answers (1)

Tiago Lopo
Tiago Lopo

Reputation: 7959

It's easy:

#!/bin/bash

email() {

        echo "Results:"
        cat 
}

for i in $(ls -1 test); do

if [ -f "test/$i" ]; then
        echo "'$i' it's a file."
else

        echo "'$i' it's a directory."
fi

done |email | mail -s "Test" [email protected]

You need the output of your test as input of email function, note that cat is just letting it pass through.

Upvotes: 1

Related Questions