I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this:
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi
The problem I'm running into is $? tends to hold the output from the echo program and not the bc call. Is there a way I save the output from the bc program into a variable?
EDIT:
Thanks for the quick replies. Here's another way of looking at the problem. Say I modified the script a little bit so it looks like this:
#!/bin/bash
NUM=$(echo "scale=25;$1/10" | bc)
if [ $? -ne 0 ]
then
echo bad
exit
fi
echo "$NUM"
When the user inputs a normal floating point value, it works fine:
bash script.sh 1.0
output:
.1000000000000000000000000
However, when the user enters an incorrect value, the script can't recover:
bash script.sh 1.0a
output:
(standard_in) 1: parse error
What I'm trying to do is get it to exit gracefully.
Are you after the result of calculation from bc (which you store in NUM) or the status return from the system call?
As I said you have the result of calculation in
$NUM
:Test:
I don't see anything wrong. $NUM is supposed to hold your
bc
command resultssee:
output
another way is to use awk
The other way, is to do the check of "$1" before you pass to
bc
. egyou don't have to check for
$?
frombc
anymoreFor GNU
bc
, an error similar to "(standard_in) 1: syntax error" will be output on stderr. You can capture this in your variable and check for it.