LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can I use a wildcard when using the mailx command? (https://www.linuxquestions.org/questions/linux-newbie-8/can-i-use-a-wildcard-when-using-the-mailx-command-4175451631/)

00mpa 02-25-2013 10:54 AM

Can I use a wildcard when using the mailx command?
 
Hi all,

I am trying to email a text file using the mailx command. I am doing the following:

#!/bin/sh

mailx -s 'Report' emailaddress < /directory_name/filename.out

The problem comes about in the fact that I will not know the exact name of the file being emailed so I wanted to use a wildcard since it changes several times per day, but I know that it will have a .out extension. Just a random FYI, I am not worried about sending the wrong file because the .out file will be the only one in the directory. Anyway, I tried doing the following, but when I try running the script below, I get an error saying "/directory_name/*.out: cannot open"

#!/bin/sh

mailx -s 'Report' emailaddress < /directory_name/*.out

Can anyone help?

Thanks!

shivaa 02-25-2013 11:07 AM

Can you once try:
Code:

~$ mailx -s 'Report' abc@example.com <(cat /directory_name/*.out)
But are you sure that there will be only one file with extension .out, irrespective of filename?

00mpa 02-25-2013 11:15 AM

Hi,

Yes, I'm positive that there will only be one .out file. The reason why I don't know the name of the file is because the file is generated several times per day, but during its generation process, it deletes all .out files before creating the new updated .out file.

00mpa 02-25-2013 11:23 AM

Also, I tried running both versions that you suggested and I get a syntax error saying '(' unexpected.

shivaa 02-25-2013 11:39 AM

It's bash specific process substitution. So either change shell to bash and then invoke it:
Code:

bash~$ bash
bash~$ mailx -s 'Report' abc@example.com <(cat /directory_name/*.out)

Or, you can try:
Code:

bash~$ find /directory_name -name "*.out" -print | xargs mailx -s 'Report' abc@example.com
bash~$ mailx -s 'Report' abc@example.com <(find /directory_name -name "*.out" -print)


00mpa 02-25-2013 12:19 PM

If I understood your advice correctly, I tried the following three things with different results for each:

First Option:

#!/bin/bash

mailx -s 'Report' abc@example.com <(cat /directory_name/*.out)

And I get a "cannot open /directory_name/*.out" error. Also, no email is sent.

Second Option:
#!/bin/bash

find /directory_name -name "*.out" -print | xargs mailx -s 'Report' abc@example.com
mailx -s 'Report' abc@example.com <(find /directory_name -name "*.out" -print)

I get a blank prompt so when I do Ctrl+C so that the blank prompt goes away, it sends the email, but it's blank.

Third Option:
find /directory_name -name "*.out" -print | xargs mailx -s 'Report' abc@example.com

Sends a blank email.

I'm sorry this may seem super basic, but I'm simply trying to learn how to do this with little background knowledge in the subject.

Thanks!

chrism01 02-25-2013 07:06 PM

Code:

cat *.out|mailx -s 'Report' abc@example.com
For more general process output eg
Code:

(uname -a) |mailx -s 'Report' abc@example.com

shivaa 02-25-2013 07:30 PM

This is working fine:
Code:

~$ mailx -s 'Report' abc@example.com < $(find /directory_name -name *.out -print)
Else you can even use:
Code:

~$ mailx -s 'Report' abc@example.com < *.out
But there should be only one file with .out extension, else you will get error like:
Code:

-bash: *.out: ambiguous redirect


All times are GMT -5. The time now is 09:19 AM.