-1

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?

tafazzi87
  • 439
  • 2
  • 5
  • 14

2 Answers2

1

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
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24
0

Try to escape the $ char inside awk with \

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}}'`
Quentin Morrier
  • 675
  • 5
  • 14