Linux中print命令中的数学表达式

时间:2015-06-12 08:27:05

标签: linux shell printf

我不能在这个命令中弄错:

a=2
b=5
c=3
printf "%.2f\t" "'$a'+'$c'*'$b'" > ofile.txt

我得到的值为50.00。但我应该得到17.00。

当a,b,c是浮动值时,如何做到这一点?例如a = 2.4,b = 5.1和c = 3.2

2 个答案:

答案 0 :(得分:6)

printf的第二个参数被解释为字符串'2',其ascii值为50。 如果你想做算术,请在bash中使用算术评估:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyHandler.class)
public class MyHandlerTest {
    @InjectMocks
    MyHandler myHandler;
    @Test
    public void testMain() throws Exception {
        Utils utils = mock(Utils.class);
        when(utils.someMethod()).thenReturn(1);
        whenNew(Utils.class).withArguments(anyInt()).thenReturn(utils);
        myHandler.someMethod();
    }
}

答案 1 :(得分:1)

printf "%.2f\t" "$(($a+$b*$c))" > yourname.txt