LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2009, 06:12 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
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.
 
Old 08-12-2009, 08:56 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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"?
 
Old 08-12-2009, 09:21 AM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Yeah you're right the title was a bit crap, any ideas how I can change it? Can't see an option anywhere
 
Old 08-12-2009, 10:07 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You can change the title if you open your first post and the click "Go advanced".

What is
Code:
delete mail | -e ARRAY$1 -e ARRAY$2 -e ARRAY$3.... | echo deleted email
intended to do? It runs delete mail and pipes stdout from it into -e ARRAY$1 ... but there is no "-e" command ???

And why does the code both loop over all the elements of the array and use all (?) the elements of the array in the pipeline above?

The first element of an array is ${ARRAY[1]}, not ARRAY$1.

This code will build what I think you want in $STRING (plus an extra space at the beginning which is probably inconsequential)
Code:
ARRAY=(a b c)
TOTAL=${#ARRAY[@]}
STRING=''
for (( i=0;i<$TOTAL;i++));
do
	STRING="$STRING -e ${ARRAY[$i]}"	
done
echo "'$STRING'"
 
Old 08-12-2009, 10:34 AM   #5
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
Old 08-12-2009, 11:01 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by genderbender View Post
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.

Last edited by catkin; 08-12-2009 at 11:10 AM.
 
Old 08-12-2009, 11:07 AM   #7
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
where are we using the i in for i in ${DOMAINS[${*}]}
 
Old 08-12-2009, 11:12 AM   #8
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
printing array contents after "push" of elements gives a different output in PERL gaynut Programming 1 08-20-2008 04:04 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
sed: howto group regex's into AND/OR clauses? jhwilliams Linux - Software 5 08-01-2007 02:19 PM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 04:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration