LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script Variable Value (https://www.linuxquestions.org/questions/linux-newbie-8/script-variable-value-4175412062/)

ehren8879 06-18-2012 10:18 AM

Script Variable Value
 
I have the following script that wipes the queue of all emails from a particular sender.

Code:
mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($7 == "spammer@foo.com") print $1 } ' | tr -d '*!' | postsuper -d -

I'd like to set this up in a shell script named qcleanse.sh and execute it with the email address defined. I tried the following but it just stares at me when I execute the script.

Code:
mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($7 == "$EMAIL") print $1 } ' | tr -d '*!' | postsuper -d -

If I do:

./qcleanse EMAIL=spammer@foo.com

it doesn't replace $EMAIL in the script with the value defined.

What am I doing wrong?

Script noob

kbscores 06-18-2012 11:51 AM

AWK is weird with Variables

Anything you put in " " will be taken literally so

"$EMAIL" is actually being sent as $EMAIL instead of variable value and since awk is local with variables anything defined outside of the awk will not be recognized.

So in order to make this work you need to make the variable appear in what you grep:

I'm not familar enough with mailq to be able to help with awk if you could post a mailq line I probably could help a bit more. What does a line look like?

Sub personal info - all i need is format.

pan64 06-18-2012 12:08 PM

if you already use awk try to use only one awk script instead of this chain of tail|grep|awk|tr.
Also would be nice to copy the output of mailq before processing and describe the result you want to get






__________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.

ehren8879 06-18-2012 12:17 PM

I was able to make this work with "'$EMAIL'" (pure trial and error perseverance!)

Decided to have it ask you for the email address first. Here's what my functional script currently looks like:

qcleanse.sh:

echo -n "What is the email address? "
read -e EMAIL
mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($7 == "'$EMAIL'") print $1 } ' | tr -d '*!' | postsuper -d -


This was created because we get spam outbreaks from time to time that fill the queue to the brim. Previously I had to execute the script manually and paste in the sender's reply-to address. This automates the process and reduces 3am fat fingering.

Thanks for the replies.


All times are GMT -5. The time now is 07:38 PM.