Portál AbcLinuxu, 12. května 2025 05:14
#!/bin/bash for file in /home/hates/maily/*; do FILE="$file" ID=`echo $file | cut -d "." -f 1-1 | cut -d "/" -f 5-5` MAIL=`grep ${ID} /home/hates/adresy.txt | cut -d " " -f 2-2` echo "Soubor $file odeslan na mail ${MAIL}" cat $file | mail ${MAIL} done;Staci malinko upravit pro tvoje potreby. mozna to jde i jednoduseji. pomoci awk primo rozparsovat ten radek v adresy.txt a potom odeslat prislusny soubor.
Za druhe vlakno se velmi omlouvam, trochu se mi to nepovedlo, nebyl to umysl.
Za skript velmi dekuji, po napasovani na moje podminky funguje velmi dobre. Mel bych jen dve poznamky.
1) Potrebuji poslat soubor jako prilohu, nikoli jako telo mailu. Neco jako:
mutt -s "soubor" -a $file $(MAIL) < default_message.txt
coz mi zatim nefunguje, ale snad to nejak doladim. Pokud by jste vedel, rad se poucim.
2)V puvodnim dotazu jsem nezminil moznost, ze pocet souboru nemusi korespondovat s poctem adres. Dalo by se to jeste zohlednit?
Diky.
#!/bin/bash # soubor s obsahem mailu musi byt ve tvaru # <jmeno>.<pripona> SOUBOR_ADRES="/home/hates/adresy.txt" SUBJECT="Automaticky odesilany mail" echo `date +%c` >> /home/hates/neodeslane_maily.txt for file in /home/hates/maily/*; do FILE="$file" ID=`echo $file | cut -d "." -f 1-1 | cut -d "/" -f 5-5` MAIL=`grep ${ID} ${SOUBOR_ADRES} | cut -d " " -f 2-2` if [ -z ${MAIL} ]; then echo "Soubor $file nebyl odeslan. Adresa s ID ${ID} neexistuje" << /home/hates/neodeslane_maily.txt else echo "Soubor $file odeslan na mail ${MAIL}" echo "Odeslan mail s ID ${ID}" | mutt -s "${SUBJECT}" -a $file ${MAIL} > $file fi done;
#!/usr/bin/python # -*- coding: utf-8 -*- import os adresy = {} for line in file('adresy.txt'): f, addr = line.split() filename = f + '.txt' try: if filename not in adresy[addr]: adresy[addr].append(filename) else: print "Duplicit line", filename, addr except KeyError: adresy[addr] = [filename] for adresa, soubory in adresy.iteritems(): att = '-a ' + ' -a '.join(soubory) print 'mutt -s "Rozeslane soubory"', adresa, attprojde seznam adres, zjistí co komu poslat, oznámí duplicity a nachystá příkaz pro odeslání. Třeba z tohohle:
001 jenda@koza.com 002 pavel@ovce.com 230 franta@jetel.com 003 pavel@ovce.com 240 franta@jetel.com 200 franta@jetel.com 240 franta@jetel.comUdělá toto:
Duplicit line 240.txt franta@jetel.com mutt -s "Rozeslane soubory" jenda@koza.com -a 001.txt mutt -s "Rozeslane soubory" pavel@ovce.com -a 002.txt -a 003.txt mutt -s "Rozeslane soubory" franta@jetel.com -a 230.txt -a 240.txt -a 200.txt
#!/usr/bin/python # -*- coding: utf-8 -*- import os subject = 'rozeslane soubory' body = 'Rozeslane soubory\n' datadir = '/home/honza/datadir' def posli(adresa, soubory): cmd = ['mutt', '-s', subject] for soubor in soubory: cmd.extend(('-a', os.path.join(datadir, soubor))) cmd.append(adresa) print cmd #w, r = os.popen2(cmd) #w.write(body) #w.close() adresy = {} souborynadisku = dict([(x, None) for x in os.listdir(datadir)]) souboryvadresach = {} for linenum, line in enumerate(file('adresy.txt')): try: f, addr = line.split() except: print 'radek %d: "%s" je divny' %(linenum, line.strip()) continue filename = f + '.txt' souboryvadresach[filename] = None if filename not in souborynadisku: print 'radek %d: soubor "%s" chybi' %(linenum, filename) continue try: adresy[addr].append(filename) except KeyError: adresy[addr] = [filename] for snd in souborynadisku: if snd not in souboryvadresach: print 'Soubor "%s" neni komu poslat' %snd adr = adresy.keys() adr.sort() for adresa in adr: soubory = adresy[adresa] posli(adresa, soubory)Ukázalo se, že je potřeba odchytit co nejvíc chybových situací (divné řádky, chybějící soubory, přebývající soubory), tak se to trochu nafouklo a na efektivitu jsem pak už přestal hledět. Ale prý to funguje
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.