Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail X command 2

Status
Not open for further replies.

keerthi2016

Programmer
Jul 20, 2015
14
IN
Currently i am sending the list of process running in the backend to the user as below.

ps -ef | grep "Srv" | mailx -s "Server Currently Running " xyz@gmail.com

Current Output :

dba 3108 17851 0 Jul 7 ? 13:36 expres63 /var/tmp/xsauthn /var/tmp/xsauthz Srv634P 27 32 33
dba 6501 17851 0 Jul 17 ? 2:34 expres63 /var/tmp/xsauthn /var/tmp/xsauthz Srv634 27 32 33 3

Expected Output :

Need to amend the below email information in the body of the email.

dba 3108 17851 0 Jul 7 ? 13:36 expres63 /var/tmp/xsauthn /var/tmp/xsauthz Srv634P 27 32 33
dba 6501 17851 0 Jul 17 ? 2:34 expres63 /var/tmp/xsauthn /var/tmp/xsauthz Srv634 27 32 33 3

Srv634P -> used for client acecssing.
Srv634 -> Used for server acessing.
 
Hi

If I understand you correctly :
Code:
[teal]([/teal]
    echo [i][green]'Need to amend the below email information in the body of the email.'[/green][/i]
    ps -ef [teal]|[/teal] grep [i][green]"Srv"[/green][/i]
    echo [i][green]'Srv634P -> used for client acecssing.'[/green][/i]
    echo [i][green]'Srv634 -> Used for server acessing.'[/green][/i]
[teal]) |[/teal] mailx -s [i][green]"Server Currently Running "[/green][/i] xyz@gmail[teal].[/teal]com

Feherke.
feherke.ga
 
yes you are right. I am receiving the output as expected.
Output Reicieved :

1. oesdba 3108 17851 0 Jul 7 ? expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634P
2. oesdba 6501 17851 0 Jul 17 ? expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634

3. efsoesvr 17644 17641 1 11:16:42 pts/2 0:00 grep Srv

Srv634P -> used for client acecssing.
Srv634 -> Used for server acessing.


Please let me know how to remove the 3rd line which we used to search in the unix command.

Thanks a lot for providing solution.
 
Hi

keerthi2016 said:
Please let me know how to remove the 3rd line which we used to search in the unix command.

There are two usual ways :
[ol]
[li]Use another [tt]grep[/tt] call to exclude lines containing "grep" :
Code:
ps -ef [teal]|[/teal] grep [i][green]'Srv'[/green][/i] [teal]|[/teal] grep -v [i][green]'grep[/green][/i]'
[/li]
[li]Use a regular expression that matches the needed string but not matches itself :
Code:
ps -ef [teal]|[/teal] grep [i][green]'[S]rv'[/green][/i]
[/li]
[/ol]


Feherke.
feherke.ga
 
hi feherka,

I need one more help from you.

Currently I am getting output as .

oesdba 3108 17851 0 Jul 7 expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634P 27 32 33
oesdba 6501 17851 0 Jul 17 expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634 27 32 33 3
ExpSrv634P Instance used for acessing
ExpSrv634 Instance used for acessing


I need to provide a space as below for the output. can you help me out.
Expected Output :

oesdba 3108 17851 0 Jul 7 expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634P 27 32 33
oesdba 6501 17851 0 Jul 17 expres63 /var/tmp/xsauthn /var/tmp/xsauthz ExpSrv634 27 32 33 3

Instance Description :

ExpSrv634P Instance used for accessing
ExpSrv634 Instance used for accessing
 
Hi

That is more difficult question, as [tt]echo[/tt] is not fully portable. So you have to pick one of the solutions that works on your system :
Code:
echo   [gray]# without parameters usually displays just a newline[/gray]

echo [i][green]'line one[/green][/i]
[i][green]line two[/green][/i]

[i][green]line four ( yepp, line three was empty and so will be line five )[/green][/i]
[i][green]'[/green][/i]      [gray]# not echo's trick, but the shell's : embed literal newlines in the parameter[/gray]

cat [teal]<<[/teal]ENDOFTEXT [gray]# similar as above, but with cat and here-document[/gray]
line one
line two

line four [teal]([/teal] yepp[teal],[/teal] line three was empty and so will be line five [teal])[/teal]

ENDOFTEXT

echo [i][green]'line one[/green][/i][lime]\n[/lime][i][green]line two'[/green][/i]   [gray]# some implementations interpret embedded \n as newlines[/gray]

echo -e [i][green]'line one[/green][/i][lime]\n[/lime][i][green]line two'[/green][/i]   [gray]# other implementations does it only when asked to by using the -e option[/gray]

echo $[i][green]'line one[/green][/i][lime]\n[/lime][i][green]line two'[/green][/i]   [gray]# somehow similar, but here Bash interprets the \n, not echo[/gray]

[b]printf[/b] [i][green]'line one[/green][/i][lime]\n[/lime][i][green]line two[/green][/i][lime]\n[/lime][i][green]'[/green][/i]   [gray]# printf is standard, just note that it outputs no implicit newline[/gray]

Feherke.
feherke.ga
 
Thanks for your solution. All the options worked out.

Cam tell me also let me know how to make the 1st line with bold and underlined.

1st line "Instance Description/"

 
Hi

keerthi2016 said:
Cam tell me also let me know how to make the 1st line with bold and underlined.
By default e-mails are plain texts, so no formatting. Some clients may honor some conventions of plain text formatting, like *bold* and _underline_, but that is each client's own decision.

What you can do, is send the content as HTML document. Then you can use some usual HTML and CSS formatting. ( Not all. And they will not appear the same way in every client. )

To tell the mail clients the content is HTML document, add those [tt]-a[/tt] parameters to [tt]mailx[/tt] as below :
Code:
[teal]([/teal]

cat [teal]<<[/teal]ENDHEADER
<html>
<body>
<pre>
ENDHEADER

    ps -ef [teal]|[/teal] grep [i][green]'[S]rv'[/green][/i]

cat [teal]<<[/teal]ENDFOOTER
</pre>
<p><b><u>Instance Description :</u></b></p>
<p>
Srv634P -&gt; used for client acecssing.<br>
Srv634 -&gt; Used for server acessing.
</p>
</body>
</html>
ENDFOOTER

[teal]) |[/teal] mailx -s [i][green]"Server Currently Running "[/green][/i] -a [i][green]'MIME-Version: 1.0'[/green][/i] -a [i][green]'Content-Type: text/html'[/green][/i] xyz@gmail[teal].[/teal]com

[small][maroon]Warning ![/maroon] The above code was not tested.[/small]

Feherke.
feherke.ga
 
thanks for your update.

I am getting the below error when I tried to execute the above code.

mailx: illegal option -- a
 
I have pasted the given code html in the name of testt.html and called the html code in the shell script.

home/unix >cat testt.html

(

cat <<ENDHEADER
<html>
<body>
<pre>
ENDHEADER

ps -ef | grep 'rv'

cat <<ENDFOOTER
</pre>
<p><b><u>Instance Description :</u></b></p>
<p>
Srv634P -> used for client acecssing.<br>
Srv634 -> Used for server acessing.
</p>
</body>
</html>
ENDFOOTER

/home/unix]$ ksh -x lkl.sh
set -x
cat testt.html

But I could see the emails are not sent to the id.
 
I am wrongly executing the html tag. it would be great if you can help me how to execute the html tag.
 
Hi

keerthi2016 said:
mailx: illegal option -- a
That is bad. That [tt]-a[/tt] option is an addition, not part of the standard. Some such tools have it, others not.

You need [tt]-a[/tt] or something equivalent to specify a mail header. The manual of the [tt]mailx[/tt] implementation on my machine describes it like this :
man mailx said:
[pre]  -a Specify additional header fields on the command line such as "X-
Loop: foo@bar" etc. You have to use quotes if the string con‐
tains spaces. This argument may be specified more than once, the
headers will then be concatenated.[/pre]

Look in the manual of your [tt]mailx[/tt] to see whether it mentions another way to specify headers.

Alternatively, check if [tt]sendmail[/tt] is available on your machine.

Feherke.
feherke.ga
 
can you please tell me how to run the file test.html file.

Previously i have run the file with the .sh extension hence i got the error.
 
Sendmail can do it. Something like this...

Code:
export SUBJECT="Servers Currently Running"
export MAILTO=xyz@gmail.com

(
cat <<-EMAILHEADER
To: ${MAILTO}
Subject: ${SUBJECT}
MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline
EMAILHEADER

cat <<-DOCHEADER
<html>
<body>
<pre>
DOCHEADER

    ps -ef | grep '[S]rv' | grep -v grep

cat <<-DOCFOOTER
</pre>
<p><b><u>Instance Description :</u></b></p>
<p>
Srv634P -> used for client accessing.<br>
Srv634 -> Used for server accessing.
</p>
</body>
</html>
DOCFOOTER

) | /usr/sbin/sendmail ${MAILTO}
 
Hi

SamBones, an empty line is missing there, right before closing the EMAILHEADER here-document, to separate e-mail headers and e-mail body.


Feherke.
feherke.ga
 
hi sambones,

Everything worked as expected. thanks for your help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top