LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2019, 09:46 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
send output of command to different email address


I am writing a loop that for each directory name it finds, will run some gcalcli commands and output calendar events. I also have a few if statements in the loop, and depending again on the directory name, it will create a variable for an email address. And then I want to send the output of the command to the correct email address.

The purpose is for each $client to receive the output of the gcalcli command on their $email.

I think I’m close, but it’s a bit confusing on how to place everything in the script.

Code:
#!/bin/bash

# variables
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

for client in $(/usr/bin/ls -d */ | /usr/bin/sed 's#/##')
do

 if [[ $client = client1_name_here ]] ; then email=client1email@ishere ; fi
 if [[ $client = client2_name_is_here ]] ; then email=client2emailaddress@ishere ; fi
 if [[ $client = client3_name_is_placed_here ]] ; then client3email@isrighthere ; fi

 echo
 echo reminders for $client on $(date +%d-%m-%y)
 echo -------------------------------------------------------
 /usr/bin/gcalcli --config-folder $dir/$client/calendar agenda --tsv \
 "$(/bin/date -d 'now')" "$(/bin/date -d 'now + 24 hours')"

 echo $email
 /usr/bin/mail -s "reminders for $(date +%d-%m-%y)" $email

done
 
Old 11-13-2019, 10:12 AM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by aristosv View Post
I am writing a loop that for each directory name it finds, will run some gcalcli commands and output calendar events. I also have a few if statements in the loop, and depending again on the directory name, it will create a variable for an email address. And then I want to send the output of the command to the correct email address.

The purpose is for each $client to receive the output of the gcalcli command on their $email.

I think I’m close, but it’s a bit confusing on how to place everything in the script.

Code:
#!/bin/bash

# variables
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

for client in $(/usr/bin/ls -d */ | /usr/bin/sed 's#/##')
do

 if [[ $client = client1_name_here ]] ; then email=client1email@ishere ; fi
 if [[ $client = client2_name_is_here ]] ; then email=client2emailaddress@ishere ; fi
 if [[ $client = client3_name_is_placed_here ]] ; then client3email@isrighthere ; fi

 echo
 echo reminders for $client on $(date +%d-%m-%y)
 echo -------------------------------------------------------
 /usr/bin/gcalcli --config-folder $dir/$client/calendar agenda --tsv \
 "$(/bin/date -d 'now')" "$(/bin/date -d 'now + 24 hours')"

 echo $email
 /usr/bin/mail -s "reminders for $(date +%d-%m-%y)" $email

done
There appears to be a bug in that third "if" statement: Aren't you missing the "email=" ??

The "mail" command will appear to hang while it's waiting for input. If all you want to do is send the "Subject", you'll want to redirect /dev/null into the command:
Code:
/usr/bin/mail -s "reminders for $(date +%d-%m-%y)" $email < /dev/null
A better, friendlier solution might be to use something like:
Code:
echo "There is new stuff on your calendar." | /usr/bin/mail -s "reminders for $(date +%d-%m-%y)" $email
If you need to include calendar items in the body of the email, you'll need to redirect those into a temporary file, redirect that file into the mail command (rather like the "/dev/null" example above), and remove the temporary file.

HTH...

Last edited by rnturn; 11-13-2019 at 10:14 AM. Reason: Added remark about calendar output.
 
Old 11-13-2019, 10:17 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
The "email=" thing was a typo, thanks for pointing it out.

The "There is new stuff on your calendar." has to be the output of this:

Code:
 echo
 echo reminders for $client on $(date +%d-%m-%y)
 echo -------------------------------------------------------
 /usr/bin/gcalcli --config-folder $dir/$client/calendar agenda --tsv \
 "$(/bin/date -d 'now')" "$(/bin/date -d 'now + 24 hours')"
And it has to be sent to the $email variable, based on the if statements.
 
Old 11-13-2019, 06:32 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
For more clarity, I'd use a case statement rather than if to map email addresses to user names.

What is this line supposed to do?
Code:
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
Obviously initializing dir with the name of a directory, but why so complicated with all the double quotes flying around?

Also, if the part before the && generates an error, dir will be empty. Rather than redirecting all messages to /dev/null, you should handle that case. Or leave out the redirection.

Right now, the output of gcalcli and the reminder string go to stdout. You should pipe this into the mail command, either using a temporary file:
Code:
echo Reminder ..... > $TEMPFILE
gclacli .... >> $TEMPFILE
mail -s Reminder $email <$TEMPFILE
or grouping the echo and gcalcli commands and using a pipe:
Code:
{
echo Reminder ..... 
gclacli .... 
} | mail -s Reminder $email
NOTE: Not tested.

Last edited by berndbausch; 11-13-2019 at 06:33 PM. Reason: formatting
 
Old 11-14-2019, 02:20 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by aristosv View Post
The "There is new stuff on your calendar." has to be the output of this:
The above would be the subject of the email.

Quote:
Code:
 echo
 echo reminders for $client on $(date +%d-%m-%y)
 echo -------------------------------------------------------
 /usr/bin/gcalcli --config-folder $dir/$client/calendar agenda --tsv \
 "$(/bin/date -d 'now')" "$(/bin/date -d 'now + 24 hours')"
The output of the above would be the body of the email. This is what should be redirected into a temp file and subsequently redirected into the mail command. I.e.,

Code:
mailx -s "There is new stuff on your calendar" joesmith@buy-n-large.com < client_event_list.tmp
rm client_event_list.tmp

...

# Loop to the next addressee...
To simplify things, I'd rewrite that bit of code as a shell function:
Code:
function get_events() {
   client=$1
   echo
   echo reminders for $client on $(date +%d-%m-%y)
   echo -------------------------------------------------------
   /usr/bin/gcalcli --config-folder $dir/$client/calendar agenda --tsv \
 "$(/bin/date -d 'now')" "$(/bin/date -d 'now + 24 hours')"
}
Then your processing loop would be something like:
Code:
declare -a CLIENTLIST                           # usually optional
CLIENTLIST=( $( code-to-generate-user-list ) )  # make array of users

for CLIENT in ${CLIENTLIST[@]}                  # loop on the client name array
do
   EMAILADDR=$( whatever code you used to obtain email address )
   TMPFILE=${CLIENT}.$RANDOM.$RANDOM.tmp        # temp file for event info

   get_events $CLIENT > $TMPFILE                # get list of events

   mailx -s "There is new stuff on your calendar" $EMAILADDR < $TMPFILE
   rm $TMPFILE                                  # cleanup the temp file

done
(I referenced "mailx" as that's what I tend to use for emailing from scripts. Adjust to your needs.)

HTH...

Last edited by rnturn; 11-14-2019 at 02:26 PM.
 
Old 11-14-2019, 02:24 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by berndbausch View Post
Code:
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
Obviously initializing dir with the name of a directory, but why so complicated with all the double quotes flying around?
Spaces in directory names? Spaces in file and directory names tend to make everything much more complicated. (Replace 'em with underscores and add years to your life.)
 
Old 11-14-2019, 09:31 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
Spaces in directory names? Spaces in file and directory names tend to make everything much more complicated. (Replace 'em with underscores and add years to your life.)
This +++++ - take it from one who has been there too many times ...
 
  


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
with a different subject send the email with a different sender on postfix NerdGZ Linux - General 1 12-15-2015 03:24 AM
echo output of command and send to email address slufoot80 Linux - Newbie 2 05-07-2013 01:38 PM
[SOLVED] allow unsubscribed email address to send email to mailman list umarzuki Linux - Server 1 07-01-2010 03:54 AM
Perl email::send.. how to send the email? hawk__0 Programming 6 12-24-2009 01:53 PM
how to send the output of cronjobs to another email address ? kirtikjr Linux - Software 1 07-12-2007 05:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:50 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