Adding and multiplying numbers using a shell script

I am learning shell scripts, I am trying to write a small script that adds and multi-digit number as shown below. But the value is not displayed

value=212
amt=`expr "( $value * 2 + ( $value * 2 * .075 ) ) " | bc`
echo $amt

      

+3


source to share


2 answers


Your given code works fine, but I intend to suggest some improvements:

  • Use in $(...)

    place of backlinks.
  • Replace expr

    with echo

    .

Example:



value=212
amt=$(echo "( $value * 2 + ( $value * 2 * .075 ) ) " | bc)
echo $amt

      

Output:

455.800

      

+1


source


First of all. It:

value * 2 + (value * 2 * .075)

      

is a hot mess, this is equivalent to:



value * 43 / 20

      

Next, I prefer to use awk for this task:

#!/usr/bin/awk -f
BEGIN {
  value = 212
  print value * 43 / 20
}

      

-1


source







All Articles