Question

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?

Was it helpful?

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

OTHER TIPS

Using of predefined variables for the fractional parts can avoid long command lines and confusion.

Infact, doing echo + awk to extract the parts seem overkill. Depending on other aspects of your module, it might be better off putting them into separate variables to begin with, rather than into $QUERY_TEN_DAYS3.

Now, supposing we're stuck with $QUERY_TEN_DAYS3, and it's a white-space delimited value in the form "DIVISOR DIVIDEND ...", another way to break it up is like so:

read -r FOO BAR JUNK <<<"$QUERY_TEN_DAYS3"

Then, we get:

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

Total Items Purchased as per EDW: $FOO

Total Items Missing or Mismatch : $BAR

Mismatch  Percentage: $((100 * ($BAR / $FOO)))

EOF

The positioning of the parentheses to do division first (truncate before multiply) seems iffy, but it's what you had above. By using variables, it's much easier to spot points like this.

Assuming this is the bash shell, you can use an expression like

$((100 * 4))

to get an evaluation of an arithmetic expression. Replace '4' with the larger expression you provided.

Please try this:

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

There are also different ways that use expr command or let command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top