Linux - NewbieThis 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
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.
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
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally Posted by aristosv
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.
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:
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.)
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.)
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 ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.