If either of the files exist then send email alert in bash shell script - linux

Trying to mock a little shell script.. There is a process that already exists which creates one of the two files (i.e, file1 or file2). My goal is to be able to send email alert depending on which file is created.
For example, if file1 exists then send email to John, David, Smith stating that file1 was created. If file2 exists then send an email to the same group of people (John, David, Smith ) stating that file2 was created.
If I write a simple if-else it will work but I will need to repeat some parts of the code which I am trying to avoid.
#!/bin/bash
file="file1"
if [ -f "$file1" ]
then
send email to three people mentioned
else
send email to three people mentioned saying file2 was created since it always creates two files
fi
}
Here I am trying to construct a script in a better way and also I don't want to repeat "send email..." commands three times because I may need to add more people into the list in the future.
Please help. Thanks in advance.

You can create a function block that send a mail to a desired list of people.Call the function from either of the case

Something like
#!/bin/bash
userlist="$HOME/myusers.cfg" # one user per line
function send_alert {
while read -r user comment; do
echo "my command to send to ${user}"
echo "subject=$1"
echo "Content=Hello ${comment}\n$2"
done < "${userlist}"
}
file="file1"
if [ -f "$file1" ]; then
send_alert "My subject" "Content: ${file1} exists."
else
send_alert "My subject" "Content: ${file2} exists."
fi
I added an optional field comment in the myusers.cfg, so you can add some comment after a space (name person, something).
You can adjust send_alert for more business rules, such as only send once an hour an alert, only send during working hours except for the person on night shift, whatever.

Related

Is there a bash function for determining number of variables from a read provided from end user

I am currently working on a small command line interface tool that someone not very familiar with bash could run from their computer. I have changed content for confidentiality, however functionality has remained the same.
The user would be given a prompt
the user would then respond with their answer(s)
From this, I would be given two bits of information:
1.their responses now as individual variables
2.the number of variables that I have now been given: this value is now a variable as well
my current script is as follows
echo List your favorite car manufacturers
read $car1 $car2 $car3 #end user can list as many as they wish
for n in {1..$numberofmanufacturers} #finding the number of
variables/manufactures is my second question
do
echo car$n
done
I am wanting to allow for the user to enter as many car manufacturers as they please (1=<n), however I need each manufacturer to be a different variable. I also need to be able to automate the count of the number of manufacturers and have that value be its own new variable.
Would I be better suited for having the end user create a .txt file in which they list (vertically) their manufactures, thus forcing me to use wc -l to determine the number of manufacturers?
I appreciate any help in advance.
As I said in the comment, whenever you want to use multiple dynamically created variables, you should check if there isn't a better data structure for your use case; and in almost all cases there will be. Here is the implementation using bash arrays. It prints out the contents of the input array in three different ways.
echo List your favorite car manufacturers
# read in an array, split on spaces
read -a cars
echo Looping over array values
for car in "${cars[#]}"
do
echo $car
done
echo Looping over array indices
for i in ${!cars[#]}
do
echo ${cars[$i]}
done
echo Looping from 0 to length-1
let numcars=${#cars[#]}
for i in $(seq 0 $((numcars-1)))
do
echo ${cars[$i]}
done

Awk can only print the whole line; cannot access the specific fields

I am currently working on my capstone project for Unix OS I. I'm very close to finishing, but I'm stuck on this part: basically, my assignment is to create a menu-based application wherein a user can enter a first and last name, I take that data, use it create a user name, and then I translate it from lowercase to uppercase, and finally store the data as: firstname:lastname:username.
When asked to display the data I must display it based on the username instead of the first name, and formatted with spaces instead of tabs. For example, it should look like: username firstname lastname. So, I've tried multiple commands, such as sort and awk, but I seem to be only able to access the fields in the file as one big field; e.g when I do awk '{print NF}' users.txt to find the number of fields per row, it will return 1, clearly showing that my data is only being entered as one field, instead of the necessary 3 I need. So my question is this: how do I go about changing the number of fields in the text file? Here is my code to add the firstname:lastname:username to the file users.txt:
userInfo=~/capstoneProject/users.txt
#make sure strings is not empty before writing to disk
if [[ "$fname" != "" && "$lname" != "" ]]
then #write to userInfo (users.txt)
echo "$fname:$lname:$uname" | tr a-z A-Z >> $userInfo
#change to uppercase using |
fi
Is it because of the way I am entering the data into my file? Using echo "$fname:$lname:$uname" ? Because this is the way my textbook showed me how to do it, and they had no trouble later on when using the sort function with specific fields, as I am trying to do now. If more detail is necessary, please let me know; this is the last thing I need before I can submit my project, due tonight.
Your input file has :-separated fields so you need to tell awk that:
awk -F':' '{print NF}' users.txt

mail can't send messages: Process exited with a non-zero status

I wrote a bash script that sends out a mail, but after 50 e-mails it starts to say "mail can't send messages: Process exited with a non-zero status". Can anyone help solve my problem. The code I used is below if you want to take a look at it.
#!/bin/bash
#Declare variables area.
emailBody=email_body.txt; #you have to use without “ symbol for some reason
emailList=email_list_delimiter.txt;
#send mail command. using a read file loop.
while IFS= read -r emailTo; do
cat $emailBody |
mail -s "Hi, I'm looking for a position in IT Field." $emailTo |
echo “Success”;
done < <(grep . $emailList)
You are probably hitting a server-side limit on the number of messages you can send in a fixed time, or equivalently the number of connections allowed within a moving window of time.
If you can (the message is not "personalized") it is best to send one message to multiple recipients, rather than many messages, each to one recipient. Do that by perhaps putting your own e-mail address in the To field, and then Bccing the whole of the list of recipients in one go. You'll have to check your mail command for how to do that.

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

How do I send an email using awk to a list of email addresses in a text file?

If I have a text file with a list of email addresses, how can I go through the list and send an email to each of those email addresses with a text file as the message.
I.e. I want to take in an email as a variable so I can execute this command:
mail -s "Welcome" email#address.com < welcome.txt
for example you have a mails_addresses.txt file with one address per line like that:
email1#mail.com
email2#mail.com
email3#mail.com
In case you have another complex structure which you need to parse with for example awk you should to show it us.
So you need just to write a loop which will read it and send it to mail command:
while read MAIL
do
mail -s "Welcome" "$MAIL" < welcome.txt
done < mails_addresses.txt
You can do this even without awk:
cat users-list | while read addr
do
mail -s "Welcome" "$addr" < welcome.txt
done

Resources