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.
As I'm starting to learn bash scripting I'm trying to automatize some tasks I usually perform. I have a notification mail I need to send several times a day. It has this structure:
read -p "Please enter username: " username
echo "Dear $username,"
Which probe to be very useful with other simple notifications like this. But I don't know how to manage the email addresses as they are usually more than one and could vary from 1 to 10. They should appear one above the other.
I found this:
"Here is a little work around. The only thing the user needs to do is hit enter without anything else on a line and it will close out"
Look at using a HERE document for the body of the email. Use a variable for the recipients name inside the HERE document, which will be expanded when the document is produced. The info manual has a section on HERE documents.
Now you just need to read the name and email address in a loop and pipe the email to a program such as `nail' to send it.
First I wanna thank you for your help. And very quick indeed! As I'm just starting with bash scripting I must recognize that your solutions are far far away of my little knowledge yet, so it will take me some time to a full understanding of every line of the code you shared with me.
Anyway, I tested your examples and they worked great. That was what I were looking for.
Now I have a couple of questions. In DrLove73's code I have to add email addresses one by one and when script is executed they appear as a list, which is OK but I wonder if there is a way to paste all addresses needed at once, separated by a field separator (space for example) with same results, I mean, make them appear as a list. I'll give you an example to be more graphical:
In this example, I put one mail after another:
Code:
[chris@agena ~/bin]$ mails2
Enter next E-mail:
user@domain.tld
Enter next E-mail:
user2@domain.tld
Enter next E-mail:
user3@domain.tld
Enter next E-mail:
user@domain.tld
user2@domain.tld
user3@domain.tld
And it gives me list which is ok, as I told you. But when I tried to add all addresses at once, they not appear as a list, like this:
Code:
[chris@agena ~/bin]$ mails2
Enter next E-mail:
user@domain.tld user2@domain.tld user3@domain.tld
Enter next E-mail:
user@domain.tld user2@domain.tld user3@domain.tld
Is there a way to add all mails at once and make them appear as a list? I tried modifying your code but still no luck.
Another question I have is about the "/n". What does it mean? What does it do? On catkin's example, on line # 4, there are plenty of them:
Quote:
text="Dear $username,"$'\n'$'\n'$'\n'
Why so many of them? Are them part of echo command? Seems that they are referring a field separator but not sure of that.
Again, thank you very much for your help and for sharing your code. And sorry for my noob questions.
#!/bin/bash
NextEmail="\n"
until [ "a$NextEmail" = "a" ];do
echo "Enter next E-mail: "
read NextEmail
for s in $(echo $NextEmail | awk '{ i=1; while(i<=NF) { print $i; i++; } }' | tr " " "\n" )
do
Emails=$Emails"\n"$s
done
done
echo -e $Emails
echo -e $Emails >> text.txt
Code:
#!/bin/bash
NextEmail="anything"
Emails=""
until [ "a$NextEmail" = "a" ];do
echo "Enter next E-mail: "
read NextEmail
if [ "a$Emails" = "a" ];then
Emails=$NextEmail
else
if [ "a$NextEmail" = "a" ];then
echo ""
else
for s in $(echo $NextEmail | awk '{ i=1; while(i<=NF) { print $i; i++; } }' | tr " " "\n" )
do
Emails=$Emails"\n"$s
done
fi
fi
echo "Current list:"
echo "-------------------"
echo -e $Emails
echo "-------------------"
done
echo "Final list:"
echo "-------------------"
echo -e $Emails
echo "-------------------"
echo "" >> text.txt # Optional
echo -e $Emails >> text.txt
echo "" >> text.txt # Optional
And do not worry about learning, only year ago I started with serious bash programing. Using only online examples I mastered even loading multi-dimensional arrays from coma separated file, something that only has few examples online. If you are dedicated learner, you will soon master it.
Last edited by DrLove73; 05-09-2010 at 04:16 AM.
Reason: removed qoutes
AWK has the concepts of "records" (separated by newlines by default) and "fields" (separated by spaces by default).
So my script initializes variable i to 1, and the while loop increments i until it is greater than the built-in variable "NF", which is the number of fields in the current record.
And $(numerical value) gets the specified field from the current record, or the whole record for 0.
Why so many of them? Are them part of echo command? Seems that they are referring a field separator but not sure of that.
As already said "\n" (not "/n") means newline in many languages. The first may have been C. Here it is used in bash ANSI-C Quoting and is effectively a string consisting of a single newline. Why so many? I figured you might like to have some space for "blah blah blah" and I didn't think of using the saner $'\n\n\n'
As I'm just starting with bash scripting I must recognize that your solutions are far far away of my little knowledge yet, so it will take me some time to a full understanding of every line of the code you shared with me.
If any of them are particularly puzzling please ask.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.