[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] something about bc
From: |
Eric Blake |
Subject: |
Re: [Help-bash] something about bc |
Date: |
Wed, 28 Dec 2011 09:13:51 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111222 Thunderbird/9.0 |
On 12/28/2011 08:44 AM, lina wrote:
> Hi,
>
> I tried but still don't know how to achieve it with *bc*
>
> some wrong try:
>
> $ a=1.5 ; a+=1 ; echo $a
> 1.51
This says make a shell variable 'a' with contents "1.5", then take those
contents and concatenate "1", for an end result of "1.51".
>
> $ a=1.5 ; a+=1 | bc; echo $a
> 1.5
This declares a shell variable a with contents "1.5", then executes a
pipeline (which spawns a subshell); in the pipeline, you pass the output
of appending to a (there is none) to bc; and since it was a subshell,
the parent shell saw no change to $a.
>
> $ a=1.5 ; let "a+=1" | bc; echo $a
> bash: let: 1.5: syntax error: invalid arithmetic operator (error token
> is ".5")
> 1.5
Like the last time, except by using 'let' instead of variable
concatenation, you are now forcing bash to attempt a numeric operation
on the contents of $a; but bash doesn't do floating point.
If you want bc to math, then pass the _entire_ bc script in to bc:
echo 'a=1.5; a+=1; print a' | bc
--
Eric Blake address@hidden +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature