LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2013, 11:54 AM   #1
00mpa
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Rep: Reputation: Disabled
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!
 
Old 02-25-2013, 12:07 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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?

Last edited by shivaa; 02-25-2013 at 12:12 PM. Reason: Checked command
 
Old 02-25-2013, 12:15 PM   #3
00mpa
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-25-2013, 12:23 PM   #4
00mpa
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Also, I tried running both versions that you suggested and I get a syntax error saying '(' unexpected.
 
Old 02-25-2013, 12:39 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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)
 
Old 02-25-2013, 01:19 PM   #6
00mpa
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
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!
 
Old 02-25-2013, 08:06 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Code:
cat *.out|mailx -s 'Report' abc@example.com
For more general process output eg
Code:
(uname -a) |mailx -s 'Report' abc@example.com

Last edited by chrism01; 02-25-2013 at 08:08 PM.
 
Old 02-25-2013, 08:30 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard/regex in bash command logicalfuzz Linux - General 2 10-27-2011 03:40 PM
Can I dereference a wildcard in a command? wjtaylor Linux - General 1 03-31-2011 11:19 AM
Using the CHOWN command with a wildcard. Skillz Linux - General 14 01-25-2010 10:42 AM
Sending mail by mailx command? lifeforce4 Debian 2 07-28-2008 06:00 PM
setting priority in mailx command linux_vidhyarthi Linux - General 1 05-20-2008 02:31 PM


All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration