Shell script to send a custom message to users listed in a file? - linux

I would like to write a shell script, that opens two text files (e.g message.txt, users.txt) and sends the message found in the first file ($1) to every single user found in the second file ($2)?
If a user is currently logged in, the message should be sent with the "write" command, else (if they are not currently logged in), it will be sent as a "mail".
I tried running this code:
#!/bin/bash
msg=`cat $1`
input=$2
while IFS= read -r usr
do
`write $usr $msg`
if [ $? -eq 1 ]
#here I tried checking if the $usr found in the $2 file is not online
`mail $usr $msg`
#The Subject part could also be a problem here
fi
done < "$input"
However, I am unsure how the mail part should be done, when running the code it even says that there is an error on line 22 (where I tried mailing the user).

You can use the following syntax to send an email via bash:
mail -s 'message subject' username#gmail.com <<< 'testing message body'
So in your case, you should add the -s parameter before the subject and you should add the body to add to your email. (you can use '' for empty body)
Another syntax to perform that:
echo "text message body" | mail -s "message subject" username#gmail.com
PS: You should not use the backticks (``) for the mail command

Related

Mail command sending the error log file as attachment instead of in the Body in Linux

I'm having an issue while sending an email from the Linux box using mail command. Instead of sending the error log as in the Body it is sending as an attachment in .bin format. Where as in few instances it is sending in the Body. Below is the log details which I'm trying to send via email as body. Is it because of Special characters like "/" in the log it is sending as .bin attachemnt. I can use sendmail inorder to fix this but we want to send it using mail command.
Caused by: org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' while uploading the file Caused by: java.io.IOException: Failed to rename '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' to /DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz'. Server replied with: 550 'PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp': cannot rename.
2021-10-15 02:01:49,342 ERROR | dbs-intg-scheduler-18 | c.n.d.j.adapter.support.MDCFatalErrorChannelInterceptor | [DBS] Fatal Error [org.springframework.integration.MessageDeliveryException: Error handling message for file [/datapp/common/batch_datafile/parts/P-16/outComingFolder/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz]] -org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_2021101507171
1.xml.gz.tmp' while uploading the file
mContent=`cat $1`
msgimp=$2
mailsub=$3
monitor=$4
logname=$5
echo >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
echo $mContent >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
mail -s "[Prod] [$monitor] [$msgimp] [$mailsub] found in $logname log" -r "DBS Production
Alert <noreply#*******.com>" alert.*********.com <
/datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
rm -f /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
mContent=`cat $1`
...
echo $mContent >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
At this point, the contents of $mailsub.txt are not the same as the contents of $1. The mContent variable is unquoted, therefore all sequences of whitespace (including newlines!) get replaced by a single space. So the body of the email is one long line.
I'm guessing here, but if that line is very long, I can imagine some part of the email system would change the message to move the body into an attachment.
I don't see why it would be necessary to read the input file, write it (incorrectly) to a new file, mail the contents of the new file, and delete the new file. Try this instead:
mData=$1
subject=$(printf "[Prod] [%s] [%s] [%s] found in %s log" "$4" "$2" "$3" "$5")
from="DBS Production Alert <noreply#*******.com>"
to="alert.*********.com"
mail -s "$subject" -r "$from" "$to" < "$mData"

adding the .txt file's content in the body of the message in the mail of mailx command in linux

I have written a script where i am storing the name of the top 10 processes which are using the greatest ram of the cpu with a command:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/etc/tmp/file.txt
Now I am attaching this file in the mailx command to send a mail with its body as content with the command:
echo -e "Warning, server memory is running low!\n\nFree memory: $free MB\n$file" | mailx -a "ALERT" -s "$subject" -r "$from" -c "$also_to" "$to"
this "subject" , "from", "also_to", "to" are some variables and have actual mailid defined in the shell script.
when I get receive the mail I get the content in a very rough order like the picture below that i have added.that mail's pic
I want this content to of the file to be in proper format or if I could just send this txt file as an attachment, I made a whole study for sending the file as an attachment using the mailx command but to no avail.
You are misusing the -a option. You are supposed to use it to add a mail header (Header: value), not the single word "ALERT" like you did.
As to how to send the file as an attachment, I usually use the -A option with no trouble (at worst, I vaguely remember that sometimes I had to specify the file encoding so that accented characters are correctly displayed, but you don't need this with your ASCII file)

How can I store the result of this command as a variable in my bash script?

I'm building a simple tool that will let me know if a site "siim.ml" resolves. If I run the command "ping siim.ml | grep "Name or service not known"" in the linux command line then it only returns text if the site does not resolve. Any working site returns nothing.
Using this I want to check if the result of that command is empty, and if it is I want to perform an action.
Problem is no matter what I do the variable is empty! And it still just prints the result to stdout instead of storing it.
I've already tried switching between `command` and $(command), and removed the pipe w/ the grep, but it has not worked
#!/bin/bash
result=$(ping siim.ml | grep "Name or service not known")
echo "Result var = " $result
if ["$result" = ""]
then
#siim.ml resolved
#/usr/local/bin/textMe/testSite.sh "siim.ml has resolved"
echo "It would send the text"
fi
When I run the script it prints this:
ping: siim.ml: Name or service not known
Result var =
It would send the text
It's almost certainly because that error is going to standard error rather than standard output (the latter which will be captured by $()).
You can combine standard error into the output stream as follows:
result=$(ping siim.ml 2>&1 | grep "Name or service not known")
In addition, you need spaces separating the [ and ] characters from the expression:
if [ "$result" = "" ]
Or even slightly more terse, just check whether ping succeeds, e.g.
if ping -q -c 1 siim.ml &>/dev/null
then
echo "It would send the text"
## set result or whatever else you need on success here
fi
This produces no output due to the redirection to /dev/null and succeeds only if a successful ping of siim.ml succeeds.

How can I build a dynamic parameter list for mail command in bash linux?

I am trying to send a mail with a file names in the message body from my terminal to my gmail account. I am using mail command to do it. My requirement is that I should dynamically update the file names in the message body as the code run...I no need to send as an attachment. I am using centos7.
Below is my script:
v_cfg_email_adresse_to="abc#gmail.com"
v_cfg_email_subject="Report from December 2016"
v_tmp_email_text_name="Message Body"
v_email_main_file="test3.sh"
v_tmp_path="/home/centos/rr/"
if [ ! -z "${v_email_main_file}" ]; then
v_mailx_parameters+=( -a "${v_tmp_path}${v_email_main_file}" )
fi
v_mail_x_parameters+=( -s "${v_cfg_email_subject}" )
v_mail_x_parameters+=( "${v_cfg_email_adresse_to}" )
printf '/binmailx %s < %s\n' "${v_mail_x_parameters[*]}" "${v_tmp_email_text_name}"
/bin/mailx "${v_mailx_parameters[#]}" < "${v_email_main_file}"
If the code is not correct..Please help me..
Thanks in advance
Instead of above code i just redirected my output in the mail command using > .I works fine!!

getting dead.letter file on Linux?

i am sending mail to list of users, i am sending mail using mailx utility
mailx -s "$SUBJECT" "$TO" < $FILE
its working fine with valid emails, but problem is that i am getting dead.letter file when i tried to send to mail id like adffadf , it string not a valid email,
expected - i want this dead.latter must not be occur even user having anything for email ID.
like abc#gmail.com, abc#def.cc, adffdfs
The man page for my mailx says a lot of things about set nosave and so on, but they don't seem to work. The only way to stop your dead.letter file growing I have found is to replace it by a link to the special file /dev/null.
rm ~/dead.letter
ln -s /dev/null ~/dead.letter

Resources