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

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

mailx -s "Data Report" -r [email protected] [email protected] <<EOF

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

EOF

In my bash Shell script I have the above code from which I am sending email. But when I check my email I always see Error Percentage written like as it is written in my above code. It is not evaluating the multiplication and division expression.

I am running the above script like this-

sh -x test1.sh

In the email I get like this-

Error  Percentage: $((100 * (183563 / 3793277)))

I am running SunOS.

May be I need to use Back-Ticks here?

Update:-

My updated script that I am using currently-

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

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

mailx -s "Data Report" -r [email protected] [email protected] <<EOF

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

EOF

I am running it like this below-

sh -c 'exec ./test.sh'

After trying the suggestion given by lot of peoples. I am getting this error-

./test.sh: command substitution: line 176: syntax error near unexpected token `/'
./test.sh: command substitution: line 176: `( 100 * $TEST2 ) / $TEST1'

What wrong I am doing now? Any thoughts?

Update:-

After making changes, it start working-

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

But in the email, the percentage I get was rounded to only one figure like 4 only instead of showing as 4.34563235 How can I get full numbers instead of getting rounded off to one digit.?

6
  • I couldn't reproduce your problem, but you might want to use ( 100 * $TEST2 ) / $TEST1 in there, otherwise you'll always get a percentage rounded to zero. Commented Oct 2, 2012 at 21:22
  • Are you certain bash is used as the shell? What does echo $SHELL give? Commented Oct 2, 2012 at 21:27
  • echo $SHELL gives me this /bin/sh... Commented Oct 2, 2012 at 21:30
  • @usta, agreed, doesn't appear that bash is being used. Ran his script under bash and sh under SunOS, works as expected under bash Commented Oct 2, 2012 at 21:32
  • IIRC, Solaris's /bin/sh is Bourne shell, not bash, at least up to Solaris 10... Commented Oct 2, 2012 at 21:50

2 Answers 2

1

You can't expect it to run under bash, if you explicitely tell sh to execute it... Try

bash -x test1.sh

Having

#!/bin/bash

as the first line, will only work if you directly execute the script:

./test1.sh

It needs to be 'executable' for that to work

chmod +x test1.sh

Update

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

needs to be

Error Percentage: $((( 100 * $TEST2 ) / $TEST1))
Sign up to request clarification or add additional context in comments.

4 Comments

If I need to explicitly tell sh to execute it, then is there any way I can run this by using sh? May be using back-ticks?
I tried your suggestion. And I am getting an error. I have updated my question. Any thoughts why is it happening?
@user1419563 yeah, too few parentheses. I've updated my answer
Thanks it worked. But I have one final question that I have updated in my question. My result is getting rounded off instead of showing full numbers.
0

Make sure that the script indeed runs with bash. The standard way to do it is to have this as the first line in your script:

#!/usr/bin/env bash

Don't have any experience with SunOS though, so not sure if that will work there or will need some slight modification.

Edit: now that it runs with bash, looks like you need it to be:

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

Update: bash does integer arithmetic only. For floating-point arithmetic you can do

Error Percentage: `echo "scale=8; ( 100 * $TEST2 ) / $TEST1" | bc`

This will use bc instead of the shell for doing the computation.

3 Comments

I have this as the first line in my script. #!/bin/bash. Will this not work?
@user1419563 It will, provided that /bin/bash exists. However, as sehe already mentioned, it won't work if you invoke the script with sh explicitly.
Updated my question with the error I am getting after making changes. Any suggestions will be of great help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.