6

I am trying to add two floating point numbers together using shell script. I have tried this:

#!/bin/bash
if [ $# != 2 ]; then
    echo "2 arguments are required "
    exit
else
    x=$1
    y=$2
    sum = $x + $y
    echo ` sum = $sum | bc `
fi

When I provide two arguments to the command line, for example:

bash filename.sh 2.4 5

... it gives me an error: [ 2 != 2 ] command not found

  • Could you please provide an example of how you are running the script? – DarkHeart Apr 21 '17 at 0:27
  • bash filename.sh 2.4 5 – Avinash Utekar Apr 21 '17 at 0:36
  • Try to execute it like ./filename.sh 2.4 5 – George Vasiliou Apr 21 '17 at 0:42
  • What is the output of type -a bash ? – DarkHeart Apr 21 '17 at 0:54
  • Are you modifying the IFS variable? – DarkHeart Apr 21 '17 at 0:55
3
else
    echo -n "sum = "
    echo "$1 + $2" | bc
fi

will fix the second half of your problem that you have not yet got to. Your first problem is a mystery. "[" is a built-in command so unless there are quotation marks you are not showing us I can't see how it can take [ $# != 2 ] as a single word.

2

Using bc:

    #!/bin/bash
    n="$@" 
    bc <<< "${n// /+}"

Supposing the script is called add, or for those who prefer easily pasted code try this workalike shell function : add() { n="$@"; bc <<< "${n// /+}"; }; it works like this:

add 3.2 5.5
add 3.2 5.5 8.9
add {1..51}.{12..89}

The curly braces use bash brace expansion to create about 4000 strings that bc interprets as decimal numbers ranging from 1.12 to 51.89.

Output:

8.7
17.6
105436.89

Note how there's no need to check for two arguments; it can add one or more arguments.

1

Your script:

#!/bin/bash
if [ $# != 2 ]; then
    echo "2 arguments are required "
    exit
else
    x=$1
    y=$2
    sum = $x + $y
    echo ` sum = $sum | bc `
fi
  1. All variable substitutions should be double-quoted: http://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
  2. Arithmetic comparison for inequality is done with [ ... -ne ... ].
  3. Diagnostic output, i.e. errors and warnings, should go to standard error.
  4. When exiting on an error condition, a non-zero exit status should be returned to the calling shell.
  5. Assignments do not accept spaces around =.

With these things in mind, your code becomes

#!/bin/sh

if [ "$#" -ne 2 ]; then
    echo >&2 'Expected two arguments'
    exit 1
fi

printf 'sum = %f\n' "$( printf '%f + %f\n' "$1" "$2" | bc )"

Alternatively, with a couple of bash extensions:

#!/bin/bash

if (( $# != 2 )); then
    echo >&2 'Expected two arguments'
    exit 1
fi

printf 'sum = %f\n' "$( bc <<<"$1 + $2" )"

With intermediate variables:

#!/bin/bash

if (( $# != 2 )); then
    echo >&2 'Expected two arguments'
    exit 1
fi

x="$1"
y="$2"
sum="$( bc <<<"$x + $y" )"
printf 'sum = %f\n' "$sum"

Modify the printf formatting string to suit your needs. If you, for example, want two decimals, use %.2f instead of %f.

0
#!/bin/bash

if [ $# -ne 2 ]; then
    echo "2 arguments are required"
else
    x=$1
    y=$2
    sum="$x + $y"
    echo "sum = `bc <<< $sum`"
fi

Saving the script to sum.sh and executing it gives this output

./sum.sh 2.45 2.55
sum = 5.00
  • The echo in backticks is a bit of a code smell. Somehow printf would feel less convoluted here. – tripleee Apr 21 '17 at 4:32
  • @tripleee Fixed. I am not a fan of using printf as on many default Linux installs printf was missing. – GMaster Apr 21 '17 at 5:48
  • 2
    Huh? It was specified by POSIX way, way back. Which Linuxes are missing it? – tripleee Apr 21 '17 at 5:55
  • 1
    @GMaster Find those default Linux installs and report to their maintainers that they are seriously broken. printf is a POSIX utility. – Kusalananda Apr 21 '17 at 6:28
0
if [ "$#" != 2 ]; then
   echo "2 arguments are required"; exit 1
else
   x=$1 y=$2
   sum="[sum=]n $x $y + 2k p"
   echo "$sum" | dc
fi

Result:

sum=7.4

Explanation:

We use the `dc` calculator by placing the two operands on the stack and
adding the two top of stack elements. And prior to adding, we place a
string `sum=` on the stack, and immediately print it which as a side effect
also removes it from the stack. The precision of the results is set to 2.
0

Use this to add two floating numbers.

echo 12.8 12.2 | awk '{print $1 + $2}'

Result:- 25

Just replace the numbers with your variables.

You may use

awk "BEGIN {print 12.8+12.2; exit}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.