Wildcard in mailx argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcard in mailx argument
# 1  
Old 06-01-2016
Wildcard in mailx argument

Hi All,

I have to send some files as attachments to an email using mailx copmmand in a shell script.

The files will be generated by some other application everyday with names starting with the literal 'Send' followed by some random sequence of characters in the filenames.

I tried using * wildcard like below:

mailx -a /dir/Send* user@domain.com

However, while executing the script, it picks up only the first file it finds matching the filename and does not pickup remaining files with the similar name format.

The number of files will be varying every day as like their filenames. Only string common in the filenames will be 'Send' in the beginning. I have to send all the files in the directory matching the filenames Send*

Can anyone please help me out? Thanks in advance.
# 2  
Old 06-01-2016
I assume the -a option means: MIME-attach the given file.
You have to also put a body text like this
Code:
echo "Hi!
please find the attached files" | mailx -a /dir/Send1 user@domain.com

For multiple attached files you must put a -a before each file name
Code:
echo "Hi!
please find the attached files" | mailx -a /dir/Send1 -a /dir/Send2 user@domain.com

I don't recall a bash builtin that could do this for all present files, but a printf sub command can do it
Code:
echo "Hi!
please find the attached files" | mailx `printf " -a %s" /dir/Send*` user@domain.com

This will fail if there is no attachment file (or non-files e.g. a directory).
A fail-safe implementation is a loop that puts up a string that is passed to mailx
Code:
attach=""
for fn in /dir/Send*
do
  [ -f "$fn" ] && attach="$attach -a $fn"
done
echo "Hi!
please find the attached files" | mailx $attach user@domain.com

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

2. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

3. UNIX for Advanced & Expert Users

sudo wildcards problem: for every argument a *-wildcard? Better solution?

Hi I allow the user tommy to run this command as root sudoCommand: /app/appname/connectors/*/*/current/bin/*With "sudo -l" he sees the sudoers, but is unable to execute. $ sudo /app/appname/connectors/zur/namename/current/bin/othername agentsvc --i --u root --sn 1m7command Sorry, user... (2 Replies)
Discussion started by: slashdotweenie
2 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

Mailx: How to send a attachment using mailx command

Hi All, Can anyone please provide the command for sending an mail with attachment using mailx command. Thanks in Advance :) Regards, Siram. (3 Replies)
Discussion started by: Sriram.Vedula53
3 Replies

6. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

7. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

8. UNIX for Dummies Questions & Answers

mailx error message : mailx: NUL changed to @

If I use the "Mail" link instead of the "mail" link to ../mailx I get this error. Mail so-n-so @whatever.com mailx: NUL changed to @ Unknown command: "postmaster" The email still goes through but i get the error. If I use "mail" it goes thru without the error. Any ideas?? (2 Replies)
Discussion started by: BG_JrAdmin
2 Replies

9. UNIX for Dummies Questions & Answers

how to check if the argument contain wildcard (*,?) ?

In a script , i would like to check if the argument ( $1, $2 inside the script) contain wildcard (*,? etc). how do i do it? > script_name arg1 arg* $1 (arg1) does not contain wildcard, but $2 (arg* )contains wildcard. how can i tell in script? i need to do this is because : if arg1... (3 Replies)
Discussion started by: gusla
3 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question