具有变量ksh的函数,如js

时间:2014-10-28 01:47:12

标签: unix ksh

在java脚本中,可以将函数编写为

function divideByThree(number) {
    return number / 3;
};

console.log(divideByThree(10));

给出3.33333的输出....

如何在ksh中编写相同的内容?

我试过了:

divideByThree(number) {
    number / 3;
};

echo divideByThree(10);

但得到了这个:

-ksh: .: syntax error: `(' unexpected

1 个答案:

答案 0 :(得分:2)

divideByThree() {
    echo $(( $1 / 3.0 ))
}

divideByThree 10
echo $(divideByThree 10)
result=$(divideByThree 10)
echo $result

Bash不支持浮点运算; Korn shell确实如此。如果省略.0中的3.0,则除法为整数除法,结果为3. return语句返回状态值;您通常使用echo来获取变量等中的捕获值。