[SOLVED] Unable to send the output of a command via email
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Rep:
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.
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.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
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.
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.
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
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
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.
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.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Original Poster
Rep:
Quote:
Originally Posted by TB0ne
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.
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).
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:
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??
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.