1

I have the following entry in a bash script:

 echo "Please see attached file" | mailx -S smtp=$smtpServer -s "Subject of the mail" -a $logfile -r "sender@domain.com" receiver1@domain.com receiver2@domain.com

It's working fine if there is no error in sending mail, but if there is an error, mailx will show the error message and wait for user to press the enter key. How can I want to avoid that? It should not wait for the user to press the enter key.

3
  • Note the mailx on my linux system uses -A for attachments and -a for headers. Jul 14, 2017 at 13:13
  • Does sending into subshell and also into background help? (command1 | command2 &>/dev/null &) ; someOtherStuffHere
    – user80158
    Jul 14, 2017 at 17:09
  • Did any of the answers solve your problem? If so, please use the checkmark to tell the system that the question is Answered. Thank you!
    – Jeff Schaller
    Jul 30, 2017 at 12:13

3 Answers 3

2

You would normally try to use the application switches for non-interactive mode or suppressing errors for this. I didn't find any explicit ones for mailx after quick skim of the docs, however.

A more general approach of doing this would be to use the timeout command for this, which runs the specified command and kills it if it is still running after specified timeout.

For example, in your case, the command would be:

echo "Mail body" | timeout 15s mailx ${MAILX_OPTIONS}

This command will kill mailx if it's still running after 15 seconds, which should be only in the case of a lingering error message.

1

In the gross-hack category of solutions, send along a gratuitous carriage return, which will be there if needed, and thrown away if not:

{ echo "Please see attached file"; echo; } | mailx ...
-1

You can run the command in an expect shell, which will watch allow you to look for prompts and send information in response.

#!/usr/bin/expect
spawn COMMAND
expect "PROMPT"
send "RESPONSE\n";
interact
1
  • 1
    This is a good idea in general, but how would the actual implementation look for this use case?
    – Jeff Schaller
    Jul 14, 2017 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.