LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 10: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, 11:07 AM   #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 11:12 AM. Reason: Checked command
 
Old 02-25-2013, 11:15 AM   #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, 11:23 AM   #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, 11:39 AM   #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, 12: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, 07:06 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 07:08 PM.
 
Old 02-25-2013, 07: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



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 02:40 PM
Can I dereference a wildcard in a command? wjtaylor Linux - General 1 03-31-2011 10:19 AM
Using the CHOWN command with a wildcard. Skillz Linux - General 14 01-25-2010 09:42 AM
Sending mail by mailx command? lifeforce4 Debian 2 07-28-2008 05:00 PM
setting priority in mailx command linux_vidhyarthi Linux - General 1 05-20-2008 01:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:09 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
Open Source Consulting | Domain Registration