0

The code below is meant to calculate the percentage completed when a file is getting dd.

percentDone=$(((varFileSize / backupSize) * 100)

I am able to debug the script running and the variables get assigned numbers. However as soon as the script runs this line percentDone remains at 0.

I have also tried this:

   percentDone=$((varFileSize / backupSize))            
   percentDone=$((percentDone * 100))

Any ideas please let me know as really struggling with this problem! Thank you!!

5
  • 3
    How about first multiply, then divide, as is usual with integers Commented Oct 19, 2016 at 18:34
  • 3
    BASH doesn't support floating point arithmetic. Use bc -l or awk Commented Oct 19, 2016 at 18:36
  • @SamiKuhmonen Division and Multiplication both have the same dominance. It also wouldn't stop the program from working either. Anubhava Thank you, I will look into this! Commented Oct 19, 2016 at 18:41
  • That's why the order matters. What is 1 / 100 * 100 and what is 1 * 100 / 100 when done with integers? That's the problem. Commented Oct 19, 2016 at 18:42
  • 1
    @anubhava: Have a look on this Pseudo floating point using pure bash Commented Oct 19, 2016 at 21:55

1 Answer 1

3

Since bash only supports integers you have to do the calculation other way around:

percentDone=$((varFileSize * 100 / backupSize))

Otherwise dividing anything less than backupSize with it will result in zero and multiplying it with anything won't help.

Sign up to request clarification or add additional context in comments.

Comments

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.