Bash Script utilisant mailx à mail en pièce jointe
-
27-10-2019 - |
Question
J'ai un script bash qui exécute une requête dans Postgres, il émet à csv. Je veux ajouter à ce script à utiliser mailx pour envoyer ce fichier .csv à un e-mail particulier.
Le problème que j'ai est-ce ne sera pas envoyer le fichier. Je peux obtenir l'e-mail pour que je sais mailx est correctement configuré. Je ne peux pas l'obtenir pour envoyer en pièce jointe. Il peut également envoyer la sortie dans le corps de l'email.
Voici donc le code.
#!/bin/bash
NOWDATE=`date +%m-%d-%Y`
PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o /tmp/folder/file-$NOWDATE.csv <<EOF
Query is here
# remove the first 2 lines of the report as they are headers
sed -i '2d' /tmp/folder/file-$NOWDATE.csv
uuencode /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" mail@gmail.com
J'ai essayé la partie mailx avec:
uuencode /tmp/folder/file-$NOWDATE.csv /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" mail@gmail.com
et
uuencode /tmp/folder/file-$NOWDATE.csv file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" mail@gmail.com
Le problème que je reçois est-il recrache cette erreur quand je lance le fichier .sh.
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
La solution
NOWDATE=`date +%m-%d-%Y`
It's up to you, but consider using ISO-8601 format, YYYY-MM-DD (%Y-%m-%d
). Among other advantages, it sorts nicely.
# remove the first 2 lines of the report as they are headers
sed -i '2d' /tmp/folder/file-$NOWDATE.csv
This doesn't remove the first two lines, it just removes the second line. Change '2d'
to '1,2d'
(but see below).
Note that this modifies the file in place.
uuencode /tmp/folder/file-$NOWDATE.csv | mailx [...]
If uuencode
is given only one file name, it reads from standard input and puts the name into its output. Your following text, "I have tried the mailx part with:" ..., indicates that you're probably aware of this -- but you haven't shown us the code that fixes that issue other than in snippets.
The error message you're getting:
uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
isn't what you'd normally get if the file doesn't exist. I don't know what would cause an "Unknown system error" like that.
But here's an alternative that (a) is a bit cleaner IMHO, and (b) doesn't require uuencode
to attempt to read the file:
#!/bin/bash
NOWDATE=`date +%m-%d-%Y` # but %Y-%d-%m is better
DIR=/tmp/folder
FILE=file-$NOWDATE.csv
RECIPIENT=user@example.com
PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o $DIR/$FILE <<EOF
... Query is here
EOF
tail -n +3 $DIR/$FILE | uuencode $FILE | \
mailx -s "Accounts No Credit Card Report for '$NOWDATE'" $RECIPIENT
Autres conseils
If the problem is with uuencode...why cant you try mailx -a option which can also attach the files to the mail. Check this link for more info.
I had the same issue. A bash script executing the query, saving the csv file and mailing it. In my case it it gave the uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error
When I executed the script using the ksh shell, it worked perfectly fine without any issues. Like this - ksh script.sh
This is just another pointer. In case uuencode gives error, then try executing it using ksh; it might work for you.