ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I create a string which is space-separated list of: all the elements of an ar
Hi guys, I've created an array which is a list of domain names, the length of the list varies depending on how much an email address is getting spammed. I need to list everything in the array within another command, I'll try and psuedo code it
Code:
#find domains being spammed in MB
ARRAY=( `cat mail.txt | grep MB | awk '{print $2}'` )
#count how many domains there are
TOTAL=${#ARRAY[@]}
for (( i=0;i<$TOTAL;i++));
#I've got code for the following, just struggling to put the contents of the array in a command
#and seperate each domain with -e, this needs to be on one line too (I can make it work with
#seperate lines by putting it in a for statement).
do
delete mail | -e ARRAY$1 -e ARRAY$2 -e ARRAY$3.... | echo deleted email
done
I'm slightly reluctant to give out the code verbatim, just need some advice on sticking clauses between each of the domains (obviously dont want one at the end).
Last edited by genderbender; 08-12-2009 at 10:31 AM.
Reason: title was poor.
Can your question correctly be re-phrased as "How can I create a string which is space-separated list of: all the elements of an array with -e between adjacent array elements"?
I guess I can always delete this later or something, here's my code so far, hopefully it will make things a little clearer and I'll look like less of an idiot.
Code:
#Get domains being hit the most, ignore total.
ARRAY=( `mailq | exiqsumm | sort -n | grep KB | grep -v TOTAL | awk '{print $5}'` )
#Put all the domains into an array
ELEMENTS=${#ARRAY[@]}
for i in ${ARRAY[${*}]}
do
#delete mail from the domains in the array.
mailq | grep -C2 -e ${ARRAY[0]} -e ${ARRAY[1]} blah...| grep 00 | awk '{print $3}' | xargs -i exim -Mrm {}
done
I think the code you provided above will work though with some tinkering, thanks once again
Last edited by genderbender; 08-12-2009 at 10:42 AM.
I guess I can always delete this later or something, here's my code so far, hopefully it will make things a little clearer and I'll look like less of an idiot.
Code:
#Get domains being hit the most, ignore total.
ARRAY=( `mailq | exiqsumm | sort -n | grep KB | grep -v TOTAL | awk '{print $5}'` )
#Put all the domains into an array
ELEMENTS=${#ARRAY[@]}
for i in ${ARRAY[${*}]}
do
#delete mail from the domains in the array.
mailq | grep -C2 -e ${ARRAY[0]} -e ${ARRAY[1]} blah...| grep 00 | awk '{print $3}' | xargs -i exim -Mrm {}
done
Thanks, that's clearer now but what is the "for i in ${ARRAY[${*}]}" for? $i is set to each "most hit" domain in turn but is not used in the mailq pipeline. The effect is to run an identical mailq pipeline, once for each domain.
Assuming you want help with "blah...". How about this for a next iteration? I have taken the liberty of using more meaningful variable names and have removed the unused $ELEMENTS. $i is still not used in the loop so the mailq pipeline is still identical on each iteration.
Code:
#Get domains being hit the most, ignore total.
DOMAINS=( `mailq | exiqsumm | sort -n | grep KB | grep -v TOTAL | awk '{print $5}'` )
GREP_OPTIONS=''
for (( i=0;i<${#DOMAINS[@]};i++));
do
GREP_OPTIONS="$GREP_OPTIONS -e ${DOMAINS[$i]}"
done
for i in ${DOMAINS[${*}]}
do
#delete mail from the domains in the array.
mailq | grep -C2 $GREP_OPTIONS | grep 00 | awk '{print $3}' | xargs -i exim -Mrm {}
done
The GREP_OPTIONS='' isn't necessary unless using set -o nounset (which is not a bad idea).
Edit: The leading space at the beginning of $GREP_OPTIONS will be parsed out by the shell before the grep command sees it.
The variable was renamed, guys got it spot on and made it much clearer. As for not having "for (( i=0;i<${#DOMAINS[@]};i++));" The code I posted was missing some lines (stuff relating to my work) so was probably just an accident.
Thanks
Last edited by genderbender; 08-12-2009 at 11:18 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.