tafazzi87 tafazzi87 - 4 months ago 21
Bash Question

Using $1 to extract some text with awk in bash script

Hi i'm trying to write a script in order to extract a line if an expression is satisfied from a text file using awk.

So if i'm using this command in the shell i've right output but on the script it doesn't works maybe because $1 is recognized like the argument given by the user on shell.

that's my script :

#!/bin/bash
declare -a email=(test@test.com)
spam=`su - zimbra -c 'cat /var/log/zimbra.log | sed -n 's/.*sasl_username=//p' | sort | uniq -c | sort -n -r' | awk '{if ($1>500){print $2}}'`
for i in "${email[@]}"
do
echo "$spam"| /opt/zimbra/postfix/sbin/sendmail $i
done


but the email sent by the script has only this body :

su - zimbra -c 'cat /var/log/zimbra.log | sed -n 's/.*sasl_username=//p' | sort | uniq -c | sort -n -r' | awk '{if (>500){print }}


How can i do this?

Answer

Try this;

#!/bin/bash
a=$1
declare -a email=(test@test.com)
spam=`su - zimbra -c 'cat /var/log/zimbra.log | sed -n 's/.*sasl_username=//p' | sort | uniq -c | sort -n -r' | awk -v var="$a" '{if (a>500){print $2}}'`
for i in "${email[@]}"
do
    echo "$spam"| /opt/zimbra/postfix/sbin/sendmail $i
done
Comments