LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2023, 06:03 AM   #1
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Unable to send the output of a command via email


Hello Folks,

I'm trying out below script to send the output of couple of commands to email, whereas, I'm getting the output of $total but not able to get the output of $lags.
Rather, it's picking up email addresses as "southbound-archiver@abc.net", "--group@abc.net", etc. from the first line.

Quote:
lags='/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group southbound-archiver'

total=`$lags | awk '{sum+=$5} END {print "Total number of lags: " sum}'`
#Print the calculated value
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<< $total \n\n $lags
Kindly advise how can I get through this and send the output over email.
 
Old 08-21-2023, 06:43 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Maybe this:
Code:
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<DONE
$total
$lags
DONE
 
1 members found this post helpful.
Old 08-21-2023, 08:43 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
if it is a script use shellcheck to find and fix errors like this.
You need to use quoting (if don't like post #2)
Code:
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<< "$total \n\n $lags"
or
Code:
echo "$total \n\n $lags" | mail -s ...
 
1 members found this post helpful.
Old 08-21-2023, 08:45 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,751

Rep: Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929
Quote:
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<< $total \n\n $lags
You need double quotes around the redirection i.e.

Quote:
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<< "$total \n\n $lags"
 
2 members found this post helpful.
Old 08-22-2023, 12:26 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Smile

Hello Folks,

Good morning!

Thanks a lot for your posts. Well, I chose first option as that gave me the desired results, at least. However, I made a little change in my script as follows, and now I'm not getting total sum and please check the below output as well.

Quote:
#Calculate the total number of lags.

lags=`/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group southbound-archiver`

#echo $lags > "lags.`date +%y%m%d%H%M%S`"

total=`$lags | awk '{sum+=$5} END {print "Total number of lags: " sum}'`

#Print the calculated total number of lags and lags for each partitions
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<DONE
$total
$lags
DONE
When I executed the script, I get below message.

Quote:
~]$ ./total.sh
Note: This will not show information about old Zookeeper-based consumers.

./total.sh: line 7: TOPIC: command not found
And here is the output I get over email, where I'm not getting total sum of lags.

Quote:
Total number of lags:

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
aux-sba_device-topology_10166 0 45880481953 45880487856 5903 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 0 15661248806 15661269841 21035 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 1 15523714232 15523732962 18730 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 2 15593015191 15593040407 25216 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 3 15761398614 15761416948 18334 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 4 15677469696 15677502835 33139 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 5 15482853826 15482869374 15548 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
Any suggestions please?
 
Old 08-22-2023, 04:52 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You changed some 'apostrophes' to `backticks` but they are different things.
Code:
#Calculate the total number of lags.

lags=`/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group southbound-archiver`

#echo $lags > "lags.`date +%y%m%d%H%M%S`"

total=`echo "$lags" | awk '{sum+=$5} END {print "Total number of lags: " sum}'`

#Print the calculated total number of lags and lags for each partitions
mail -s 'Southound Lags and total number' satyaveer.arya@abc.net <<DONE
$total
$lags
DONE
 
1 members found this post helpful.
Old 08-22-2023, 05:00 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
a missing echo
Otherwise would be better to use $( something ) instead of ` something `
it is more readable.
 
1 members found this post helpful.
Old 08-22-2023, 05:47 AM   #8
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hello,

Thanks! Your posts were really helpful here.

I corrected it again with some additional requirements and changes. Please have a look below.

Quote:
#Using lags variable for storing the values.
lags=`/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group southbound-archiver`

#Calculating the total sum of lags accumulated.
total=`echo "$lags" | awk '{sum+=$5} END {print "Total number of lags: " sum}'`

#Total sum of lags
total_sum=` echo "$lags" | awk '{sum+=$5} END {print sum}'`

#Conditional check, if qualified then print the calculated sum of lags and lags against each partitions.
if [ $total_sum > 2500000 ]
then
mail -s 'Southbound Lags and Total sum' Satyaveer.Arya@abc.net <<DONE
Date: `TZ=UTC-4 date -R`

$total

Verify the lags and take necessary action/s if required.

Lags per topic:
$lags
DONE
fi
Now here, the script is running fine but when it checks the condition in if statement, it's sending out the email even if total sum of lags are less than 2500000. Could you please advise further and correct me?

The output I got over email is as follows:

I've trimmed the output for shorter message.

Quote:
Date: Tue, 22 Aug 2023 14:39:42 +0400

Total number of lags: 1172830

Verify the lags and take necessary action/s if required.

Lags per topic:

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
aux-sba_device-topology_10166 0 45892068395 45892068395 0 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 0 15664582211 15664602382 20171 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
.
.
.
.
.
.
incoming-device-events_10166 1 15526999445 15527021024 21579 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
Thanks and regards,
 
Old 08-22-2023, 06:12 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Character ">" means redirection in shell, use -gt:
Code:
Bad:  if [ 10 >   20 ]; then echo Then; else echo Else; fi
Good: if [ 10 -gt 20 ]; then echo Then; else echo Else; fi
PS: please learn the difference between [quote] and [code] tags

Last edited by NevemTeve; 08-22-2023 at 06:13 AM.
 
1 members found this post helpful.
Old 08-22-2023, 07:37 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,698

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by Satyaveer Arya View Post
Hello,
Thanks! Your posts were really helpful here.I corrected it again with some additional requirements and changes. Please have a look below.
Code:
#Using lags variable for storing the values.
lags=`/opt/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group southbound-archiver`

#Calculating the total sum of lags accumulated.
total=`echo "$lags" | awk '{sum+=$5} END {print "Total number of lags: " sum}'`

#Total sum of lags
total_sum=` echo "$lags" | awk '{sum+=$5} END {print sum}'`

#Conditional check, if qualified then print the calculated sum of lags and lags against each partitions.
if [ $total_sum > 2500000 ]
then
mail -s 'Southbound Lags and Total sum' Satyaveer.Arya@abc.net <<DONE
Date: `TZ=UTC-4 date -R`

$total

Verify the lags and take necessary action/s if required.

Lags per topic:
$lags
DONE
fi
Now here, the script is running fine but when it checks the condition in if statement, it's sending out the email even if total sum of lags are less than 2500000. Could you please advise further and correct me? The output I got over email is as follows: I've trimmed the output for shorter message.
Code:
Date: Tue, 22 Aug 2023 14:39:42 +0400
Total number of lags: 1172830
Verify the lags and take necessary action/s if required.
Lags per topic:

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
aux-sba_device-topology_10166 0 45892068395 45892068395 0 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 0 15664582211 15664602382 20171 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
incoming-device-events_10166 1 15526999445 15527021024 21579 southbound-archiver-21e4674c-3022-4f68-ba46-0756dcfa35ee/10.230.XXX.XXX southbound-archiver
You've been scripting for more than *ELEVEN YEARS* now, and this is very similar to other of your scripting threads. Odd that someone who claims to have an RHCE, Double VCP's, and AIX & CCNA certifications would need help with a bash script. Have you looked into just using the kafka API, which could get you results as well??

And the software you're using for this command seems to be something spammers use quite often.
 
Old 08-23-2023, 12:04 AM   #11
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by TB0ne View Post
You've been scripting for more than *ELEVEN YEARS* now, and this is very similar to other of your scripting threads. Odd that someone who claims to have an RHCE, Double VCP's, and AIX & CCNA certifications would need help with a bash script. Have you looked into just using the kafka API, which could get you results as well??

And the software you're using for this command seems to be something spammers use quite often.
Hi,

Thanks for your post!

I agree that I'm RHCE Certified (long back), Double VCPs, AIX and CCNA certified. However, I'm not using shell scripting quite often and now I'm exploring different areas which uses Apache Kafka and other applications.

By the way, I would advise you rather than pointing out all these things for anyone, it would be better that if you can help someone for something, help them out. That would look much better and will save your time.

Thanks and regards,
 
Old 08-23-2023, 04:27 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
Quote:
Originally Posted by Satyaveer Arya View Post
Hi,

Thanks for your post!

I agree that I'm RHCE Certified (long back), Double VCPs, AIX and CCNA certified. However, I'm not using shell scripting quite often and now I'm exploring different areas which uses Apache Kafka and other applications.

By the way, I would advise you rather than pointing out all these things for anyone, it would be better that if you can help someone for something, help them out. That would look much better and will save your time.

Thanks and regards,
Actually knowing your skills will help us to give better help to you.
For example if you really have all those certificates you don't need help to find a missing echo or to compare two numbers. But anyway you can use shellcheck to catch these kind of problems easily and you do not need to wait a minute for the correct answer (which will save your time and our times too).
 
1 members found this post helpful.
Old 08-23-2023, 05:15 AM   #13
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hello Folks,

Since this post helped me in resolving my queries, therefore, I'm marking this thread as closed.

Thanks a lot to all!

Thanks and regards,
 
Old 08-23-2023, 06:42 AM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,698

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by Satyaveer Arya View Post
Hi, Thanks for your post!

I agree that I'm RHCE Certified (long back), Double VCPs, AIX and CCNA certified. However, I'm not using shell scripting quite often and now I'm exploring different areas which uses Apache Kafka and other applications.

By the way, I would advise you rather than pointing out all these things for anyone, it would be better that if you can help someone for something, help them out. That would look much better and will save your time.
My time is my own to do with what I want. And if you're concerned about your time, wouldn't it have been faster for someone with over a decade of experience to write a few lines of a bash script, without posting to a forum asking others to do it for them?? Especially since you had these same types of questions/answers many times previously:

https://www.linuxquestions.org/quest...server-927079/
https://www.linuxquestions.org/quest...2011-a-917835/
https://www.linuxquestions.org/quest...pt-4175416277/
https://www.linuxquestions.org/quest...on-4175450618/

Why, with your 'certifications' can't you troubleshoot a script??? If you can't use shellcheck (as suggested by pan64), or do basic shell scripting (covered in great depth by any of the thousands of bash scripting tutorials), would you move on to more complicated things like the Kafka system??? Care to address what you're using Kafka for, if not for spamming??

Last edited by TB0ne; 08-23-2023 at 07:01 AM.
 
Old 08-31-2023, 12:49 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,363

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
In post #8, '>' in this context is string comparison, not arithmetic... https://linuxize.com/post/how-to-com...rings-in-bash/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo output of command and send to email address slufoot80 Linux - Newbie 2 05-07-2013 01:38 PM
[SOLVED] how to send the output of send command to a file auma78 Linux - Newbie 2 01-23-2011 12:06 AM
Command based email client to send email through secure smtp havolinec Linux - Newbie 2 07-27-2010 07:40 AM
Perl email::send.. how to send the email? hawk__0 Programming 6 12-24-2009 01:53 PM
Ned to send command output to email Thaidog Programming 2 11-21-2009 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration