How to Send Shell script Result to Email

Here are the commands you need to send the shell script output to email. Scripts people use to automate tasks. Sending script output is easy. Here are the steps to send an email using the Mailx command.

How to send shell script output to email (Mailx)

  1. Writing script with logic the output to send to a file
  2. Sending the file data to email


Bash script output to email

1. Writing script with logic the output to send to a file


Create an empty script

Create test1.sh script using the vi command with the sudo command.

$sudo vi test1.sh


Write the first line in the script


#!/bin/bash
exec > out1.txt 2>&1


Add logic in the shell script


#!/bin/bash
exec > out1.txt 2>&1

echo" Here is my logic"

Provide executable permissions to your script


$ sudo chmod +x test1.sh


Run your script using the command below

$ ./test1.sh


Verify the output


$ cat out1.txt


2. Sending the file data to email

Now, you need to send the output email.


$ mail <<output file path name>> abc@gmail.com


Bottom-line

Here, it is shown how to create a script and sending the script output to a files, and to send the output to email.


References

Post a Comment

Thanks for your message. We will get back you.

Previous Post Next Post