Sherry Sherry - 2 months ago 20
Bash Question

Mail file as well as output

I have a python script which prints some data and also writes data to a file as well.

I wanted to mail both the stuff from the linux terminal.

I am using the below command.

python script.py | mail -s "subject" -a filename.txt example@gmail.com


It mails me the output( print one ), but not the file at that time.
Gives an error no such file or directory(because its being created at that time )

The next time I run this it mails me both the stuff, as the file is there.

How Can I accomplish this task ( mail both the stuff ), when I ran the linux command for the first time.

Thanks

Answer

Don't pipe the Python script to mail instead redirect the output to a temporary file and then cat that temporary file piping it to the mail command.

For instance:

python script.py > temp-file.out
cat temp-file.out | mail -s "subject" -a filename.txt example@gmail.com