By creating a concatenated file in unix, you send this file to all of you in the same script
I am trying to create a script that will concatenate all the files out.*
in a directory /home/rfranklin/stackDump/
and then pipe that concatenated file to the mailx command so that I can send it to myself.
I have tried two methods so far and none of them seem to work. Hopefully someone can tell me why!
So far in the directory /home/rfranklin/stackDump/
I have files:
out.file1
out.file2
out.file3
out.file4
otherfile.txt
otherfile2.txt
First of all I tried to write a for loop:
#!/bin/bash
#
OUT_FILES_DIRECTORY="/home/rfranklin/stackDump/out.*"
for file in $OUT_FILES_DIRECTORY
do
cat $file > stack_dump_`date +%Y%m%d` | mailx -s stack_dump_`date +%Y%m%d` rfranklin@gmail.com
done
This returns:
Null message body; hope that ok
Null message body; hope that ok
Null message body; hope that ok
Null message body; hope that ok
And I get 4 blank emails. BUT the concatenated file is being generated, so I know something is working.
Next, I tried to use the doc here:
#!/bin/bash
#
bash <<'EOF'
cd /home/rfranklin/stackDump
cat out.* > stack_dump_`date +%Y%m%d` | mailx -s stack_dump_`date +%Y%m%d` rfranklin@gmail.com
done
EOF
This doesn't work for me either. Where am I going wrong!
thank
source to share
You can use tee
for this:
#!/bin/bash
d=$(date +%Y%m%d)
for file in /home/rfranklin/stackDump/out.*
do
cat "$file" | tee -a "stack_dump_$d" | mailx -s "stack_dump_$d" rfranklin@gmail.com
done
tee
copies standard input to a file as well as to standard output. The parameter is -a
appended to the file, not overwriting it.
In the original version, the script was >
redirecting the output of a file cat
to a file, which meant the pipe was mailx
empty.
I am assuming your script has been down for more than a day, so I moved the calls to date
outside of the loop.
source to share
I see no point in creating a file here, you could just as easily output the output cat
tomailx
cat /home/rfranklin/stackDump/out.* |
mailx -s "stack_dump_$(date +%Y%m%d)" rfranklin@gmail.com
If you prefer to attach to content in the body of the email
cat /home/rfranklin/stackDump/out.* |
uuencode "stack_dump_$(date +%Y%m%d)" |
mailx -s "stack_dump_$(date +%Y%m%d)" rfranklin@gmail.com
source to share
From what I understand, you want to concatenate all the files in a given directory into one file and then send that to yourself. Correct me if I am wrong.
So, first, combine all the files into one:
cat /home/rfranklin/stackDump/out.* > concatFile
Then write this to yourself:
dat=$(date +%Y%m%d)
mail -s "stack_dump_$dat" rfranklin@gmail.com < concatFile
Edit
You can put it in your script:
dir=$1
cat $dir/out.* > concatFile
dat=$(date +%Y%m%d)
mail -s "stack_dump_$dat" rfranklin@gmail.com < concatFile
run it like this:
./script /home/rfranklin/stackDump
concatFile
will be created in your current directory.
source to share