Search code examples
bashshellmailx

Multiplication in shell script while sending mail using mailx


mailx -s "Report for $DATE_TENDAYS_FORMAT1" -r uname@host.com uname@host.com <<EOF

Total Items Purchased as per EDW: `echo $QUERY_TEN_DAYS3 | awk '{print $1}'`

Total Items Missing or Mismatch : `echo $QUERY_TEN_DAYS3 | awk '{print $2}'`

Mismatch  Percentage: `100*((echo $QUERY_TEN_DAYS3 | awk '{print $2}')/(echo $QUERY_TEN_DAYS3 | awk '{print $1}'))`

EOF

Whenever I try to send an email using mailx with the above syntax, I always get error on third line (Mismatch Percentage) on * sign. Anything wrong I am doing in the multiplication process in third line?

Update:-

TEST1=`echo $QUERY_TEN_DAYS3 | awk '{print $1}'`
echo $TEST1

TEST2=`echo $QUERY_TEN_DAYS3 | awk '{print $2}'`
echo $TEST2

mailx -s "Report for $DATE_TENDAYS_FORMAT1" -r uname@host.com uname@host.com <<EOF
Comparison using previous day’s data

******************************************

Mismatch  Percentage: $((100 * ($TEST2/ $TEST1)))

EOF

I have tried the above code and still it gives me error on the first line as ( unexpected. Any thoughts why is it happening?


Solution

  • Works for me:

    So the problem could be any number of things, such as not actually using bash (or a recent version of it), not running what you've posted here, incorrect input, problems with the EOL character in your script file.

    $ echo $BASH_VERSION
    3.00.15(1)-release
    
    $ QUERY_TEN_DAYS3="1 2"
    
    $ TEST1=`echo $QUERY_TEN_DAYS3 | awk '{print $1}'`
    $ echo $TEST1
    1
    
    $ TEST2=`echo $QUERY_TEN_DAYS3 | awk '{print $2}'`
    $ echo $TEST2
    2
    
    $ cat <<EOF
    > Comparison using previous day’s data
    >
    > ******************************************
    >
    > Mismatch  Percentage: $((100 * ($TEST2/ $TEST1)))
    >
    > EOF
    Comparison using previous day’s data
    
    ******************************************
    
    Mismatch  Percentage: 200