Friday, 12 February 2016

How To Send Email In HTML Formatting In Unix/Linux

In This article we will see how to send an email notification with HTML formatting. We will see how to create and send following table type structure in an email body.


#----------------------------------------START OF THE SCRIPT--------------------------------------
$ cat Email_In_Html_Format.sh

#!/usr/bin/sh

V_MAIL_TO="Director@kw.com"
V_EMAIL_BODY="Email_Body.html"

#--Below code will be used to generate the HTML code
echo "<html>" > ${V_EMAIL_BODY}
echo "<head></head>" >> ${V_EMAIL_BODY}
echo "<body>" >> ${V_EMAIL_BODY}
echo "<STYLE TYPE="text/css">" >> ${V_EMAIL_BODY}
echo "<!--"  >> ${V_EMAIL_BODY}
echo "TD{font-family: Arial; font-size: 9pt;}" >> ${V_EMAIL_BODY}
echo "--->"  >> ${V_EMAIL_BODY}
echo "</STYLE>" >> ${V_EMAIL_BODY}

#--Below echo is used to create a HTML table
echo "<TABLE border="2" frame="border" rules="all">" >> ${V_EMAIL_BODY}

#--Below echo is used to create table header.
echo "<tr><th bgcolor=#BDBDBD><font size="2">EMP_ID</font></th>
<th bgcolor=#BDBDBD><font size="2">EMP_NAME</font></th>
<th bgcolor=#BDBDBD><font size="2">EMAP_DEPT</font></th>
<th bgcolor=#BDBDBD><font size="2">EMP_SAL</font></th></tr>" >> ${V_EMAIL_BODY}

#--Below echo is used to generate the data in tabular format.
echo "<tr><td>1001</td><td>A1</td><td>HR</td><td>50000</td></tr>          <tr><td>1002</td><td>A2</td><td>MAN</td><td>60000</td></tr>
      <tr><td>1003</td><td>A3</td><td>VP</td><td>100000</td></tr>         <tr><td>1003</td><td>A3</td><td>CEO</td><td>150000</td></tr>" 
      >> ${V_EMAIL_BODY}
echo "</table>" >> ${V_EMAIL_BODY}
echo "</body>" >> ${V_EMAIL_BODY}
echo "</html>" >> ${V_EMAIL_BODY}

#--Below code will be used to send email notification
export V_CONTENT="${V_EMAIL_BODY}"
(
echo "Subject: Employees Report"
echo "To: ${V_MAIL_TO}"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
echo ${V_CONTENT}

) | /usr/sbin/sendmail ${V_MAIL_TO}

#-------------------------------------------END OF THE SCRIPT---------------------------------------

Output: Above Script will send the email to the user Director@kw.com with the tabular format. We can use such emails in notifying details with more clarification and readability.

Keep Reading, Keep Learning, Keep Sharing..!!!

How To Send Email In Unix

In This post we will see how to send email using mailx command available in most of the Unix like operating system.

SYNTAX: mailx <Switch> "<Subject>" <Email Of Address Recipient>

Email With subject

mailx -s "Test" "A1@kw.com"

Output: Will send an email to A1@kw.com with subject line as test
To:  A1@kw.com

Email With CC and BCC 

We can also use cc and bcc email option by using -c and -b

mailx -s "test" -c "A2@kw.com" -b "A3@kw.com" "A1@kw.com"

Output: Email recipients will appear as shown below in email. 
To:  A1@kw.com
Cc:  A2@kw.com
Bcc: A3@kw.com

Email With body

We can do that in multiple ways:

echo "Hello User" | mailx -s "test" "A1@kw.com"

Output:In this case, email body will have following text: "Hello User"
  
We can also use the content of a file as an email body, for example we have a file F_Msg_File.txt

cat F_Msg_File.txt
Hi User,

Please Note, We will be discussing mailx command.

Thanks
Knowledge Warehouse
   
(cat F_Msg_File.txt) | mailx -s "test" "A1@kw.com"

[OR]

mailx -s "test" "A1@kw.com" < F_Msg_File.txt

Output: Above command will send an email to the user with the file content as email body.

Email With Attachment

We can attach(log file or anything) an attachment in the email by using uuencode command.

(uuencode F_Log_File.txt F_Log_File.txt) | mailx -s "test" "A1@kw.com"

Email With Attachment & body

(cat F_Msg_File.txt; uuencode F_Error_File.txt F_Error_File.txt) | mailx -s "test" A1@kw.com

Output: Will attach F_Error_File.txt in email as attachment and display the content of F_Msg_File.txt as email body.

Note: Observe carefully, we have provided F_Log_File.txt wice in uuencode command.


Keep Reading, Keep Learning, Keep Sharing...!!

How To Use Alias In Unix/Linux

Many a times we face situation where we have to move in different directories to analyse the issue or checkout the multiple objects involved. At times it becomes very lengthy and time consuming as you have type a lot to change the directories involved.

For example, let say you have following directories available for your project:
  • Source File Directory which keeps the source file
  • Target File Directory which keeps the files generated after some processing.
  • Scripts, executable logics which perform some action
  • Log File Directory, keeps the log of all the activities
Now, if you have are doing some impact analysis it will become very time consuming to navigate in these directories. Alias command provides solution of such situations. How? Let see...!!

Source File Directory: /home/baba/kw/SourceFiles/ABC
Target File Directory: /home/baba/kw/TargetFiles/ABC
Script File Directory: /home/baba/kw/Scripts/ABC
Log    File Directory: /home/baba/kw/Logs/ABC

alias Source='cd /home/baba/kw/SourceFiles/ABC'
alias Target='cd /home/baba/kw/TargetFiles/ABC'
alias Script='cd /home/baba/kw/Scripts/ABC'
alias Log='cd /home/baba/kw/Logs/ABC'
  
Now if you type log at terminal, it will directly take you to the log directory, similarly if you want to go to source directory while you are still in log directory, just type source
you will be in source file directory.

alias command is active till the session is active. if you want it to be permanent declare it in user profile file (.profile  or bash profile)


It is not like you can only navigate in directory using alias, you can almost use it in all the repetitive work. For example, you want to count number of line in a file.

alias cnt='wc -l'

$ cnt F_Input_File

It will return you the number of line available in F_Input_File

alias clr='clear'

now by only typing c your screen will be cleared.

If you want to remove alias, use unalias command to remove the same.

unalias clr

will remove the alias clr.


Conclusion: You can create alias for almost all the day to day activity so utilize this feature to make your life ease....!!!

Keep Reading, Keep Learning, Keep Sharing...!!

Thursday, 11 February 2016

How To Rename Multiple Files In Unix/Linux

Let say we have below file in home directory /home/baba

$ ls -ltr


-rw-r--r--  1 baba None   9 Jan 26 14:11 A1.txt
-rw-r--r--  1 baba None  32 Jan 26 14:20 A2.txt
-rw-r--r--  1 baba None  32 Jan 26 14:20 A3.txt
-rw-r--r--  1 baba None  10 Jan 26 14:20 A4.txt
-rw-r--r--  1 baba None  92 Feb  2 22:38 A5.txt
-rw-r--r--  1 baba None 278 Feb  2 22:43 A6.txt

We want to rename all these files and want to concatenate _bkp in file name

#!bin/sh
for i in `ls`
do
echo "Current File Name is...:"${i}
mv ${i} ${i}"_bkp"
done

Output:Will rename all the objects in the current directory by concatenating _bkp

We can archive the multiple files in one go by keeping the original fles, for example we want to keep archive of files in archive directory.

#!bin/sh
for i in `ls`
do
echo "Current File Name is...:"${i}
cp ${i} /home/baba/Archive/${i}"_bkp"
done

Output:
cp command will create a copy of the existing file in archive directory and will keep the original file in same directory.


Always use the path while reading the file name in for loop instead of giving only ls command.

#!bin/sh
for i in `ls /home/baba`
do
echo "Current File Name is...:"${i}
mv ${i} /home/baba/Archive/${i}"_bkp"
done


Output: Above all command will rename all the files from /home/baba and move it to /home/baba/Archive/ directory.

-rw-r--r--  1 baba None   9 Feb 12 14:11 A1.txt_bkp
-rw-r--r--  1 baba None  32 Feb 12 14:12 A2.txt_bkp
-rw-r--r--  1 baba None  32 Feb 12 14:13 A3.txt_bkp
-rw-r--r--  1 baba None  10 Feb 12 14:15 A4.txt_bkp
-rw-r--r--  1 baba None  92 Feb 12 14:30 A5.txt_bkp
-rw-r--r--  1 baba None 278 Feb 12 14:40 A6.txt_bkp

Keep Reading, Keep Learning, Keep Sharing.

Tuesday, 2 February 2016

How To Read A Delimited File In Unix Script

In this article we will see how to read a delimited files in shell script. We will use input fileF_Input_File.txt for demonstration purpose.

Input File: F_Input_File.txt
1001|A1|HR|5000|Y
1002|A2|FIN|6000|Y
1003|A3|HR|6000|Y
1004|A4|HR|7000|Y


We can read a file using loop (while, for), here we are reading data from F_Input_File.txt.
V_Line holds the complete record and by using cut we are fetching all the fields.

while read V_Line 
do

V_Id=`echo ${V_Line} | cut -d"|" -f1`
V_Name=`echo ${V_Line} | cut -d"|" -f2`
V_Dept=`echo ${V_Line} | cut -d"|" -f3`
V_Sal=`echo ${V_Line} | cut -d"|" -f4`
V_Actv_Ind=`echo ${V_Line} | cut -d"|" -f5`

echo "Employee Id is.........:" ${V_Id}
echo "Employee Name is.......:" ${V_Name}
echo "Employee Department is.:" ${V_Dept}
echo "Employee Salary is.....:" ${V_Sal}
echo "Employee Status is.....:" ${V_Actv_Ind}

done < F_Input_File.txt

Output:
Employee Id is.........: 1001
Employee Name is.......: A1
Employee Department is.: HR
Employee Salary is.....: 5000
Employee Status is.....: Y
Employee Id is.........: 1002
Employee Name is.......: A2
Employee Department is.: FIN
Employee Salary is.....: 6000
Employee Status is.....: Y
Employee Id is.........: 1003
Employee Name is.......: A3
Employee Department is.: HR
Employee Salary is.....: 6000
Employee Status is.....: Y
Employee Id is.........: 1004
Employee Name is.......: A4
Employee Department is.: HR
Employee Salary is.....: 7000
Employee Status is.....: Y


We can get the same output by using IFS keyword (Internal Field Separator), here data is stored in associated variables separated by pipe(|). By using IFS, we can reduce the code and implement the same with the ease.

IFS="|"
while read V_Id V_Name V_Dept V_Sal V_Actv_Ind
do

echo "Employee Id is.........:" ${V_Id}
echo "Employee Name is.......:" ${V_Name}
echo "Employee Department is.:" ${V_Dept}
echo "Employee Salary is.....:" ${V_Sal}
echo "Employee Status is.....:" ${V_Actv_Ind}

done < F_Input_File.txt

Output:
$ sh a.sh
Employee Id is.........: 1001
Employee Name is.......: A1
Employee Department is.: HR
Employee Salary is.....: 5000
Employee Status is.....: Y
Employee Id is.........: 1002
Employee Name is.......: A2
Employee Department is.: FIN
Employee Salary is.....: 6000
Employee Status is.....: Y
Employee Id is.........: 1003
Employee Name is.......: A3
Employee Department is.: HR
Employee Salary is.....: 6000
Employee Status is.....: Y
Employee Id is.........: 1004
Employee Name is.......: A4
Employee Department is.: HR
Employee Salary is.....: 7000
Employee Status is.....: Y

Conclusion: By using similar method we can read any kind of delimited file like CSV file (comma delimited)

Keep Reading, Keep Learning, Keep Sharing...!!!
Related Posts Plugin for WordPress, Blogger...