shell脚本算术运算
一般来说,我会在 shell 脚本中使用 expr 来进行算术运算。
有没有一种方法可以让我们在 shell 脚本中进行算术运算而不使用 expr?
in general i will use expr inside shell scripts for doing arithmetic operations.
is there a way where we can come up with arithmetic operation in a shell script without using expr?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现代 shell(在我看来,符合 POSIX = 现代)支持算术运算:
+ - / * 对有符号长整型变量 +/- 2147483647。
使用 awk 实现双精度、15 位有效数字它也可以执行 sqrt。
使用 bc -l 将精度扩展至最多 20 位有效数字。
您已经看到的 shell 语法 (zed_0xff):
awk 本身执行双精度 - 浮点 - 算术运算。
它还具有 sqrt、cos、sin ....请参阅:
http:// /people.cs.uu.nl/piet/docs/nawk/nawk_toc.html
bc 有一些已定义的函数和扩展精度,可通过 -l 选项使用:
bc -l
示例:
Modern shells (POSIX compliant = modern in my view) support arithmetic operations:
+ - / * on signed long integer variables +/- 2147483647.
Use awk for double precision, 15 siginificant digits It also does sqrt.
Use bc -l for extended precision up to 20 significant digits.
The syntax (zed_0xff) for shell you already saw:
awk does double precision - floating point - arithmetic operations natively.
It also has sqrt, cos, sin .... see:
http://people.cs.uu.nl/piet/docs/nawk/nawk_toc.html
bc has some defined functions and extended presision which are available with the -l option:
bc -l
example:
如果您使用 ksh,您是否尝试过阅读“man ksh”?
例如,“man bash”有足够的关于使用 bash 进行算术的信息。
命令 typeset -i 可用于指定变量必须被视为整数,例如 typeset -i MYVAR 指定变量 MYVAR 是整数而不是字符串。在 typeset 命令之后,尝试为变量分配非整数值将会失败:
要对变量或在 shell 脚本中执行算术运算,请使用 let 命令。 let 将其参数计算为简单的算术表达式。例如:
上面的表达式也可以写成如下:
$(( 和 )) 中包含的任何内容都会被 Korn shell 解释为算术表达式
Did you tried to read "man ksh" if you're using ksh?
"man bash", for example, has enough information on doing arithmetics with bash.
the command typeset -i can be used to specify that a variable must be treated as an integer, for example typeset -i MYVAR specifies that the variable MYVAR is an integer rather than a string. Following the typeset command, attempts to assign a non integer value to the variable will fail:
To carry out arithmetic operations on variables or within a shell script, use the let command. let evaluates its arguments as simple arithmetic expressions. For example:
The expression above could also be written as follows:
Anything enclosed within $(( and )) is interpreted by the Korn shell as being an arithmetic expression
shell 内置
$(())
的精度和范围取决于平台。在 32 位平台上,它仅限于 32 位整数(与expr
不同,后者似乎以独立于平台的方式支持更大的整数)。(注意 - 请注意,虽然
^
在大多数 shell 中是有效的运算符,但它并不意味着“的幂”,而是 XOR。)此外 - 有些 shell 在这方面更容易实现。例如,Zsh 允许浮点运算,例如
$(( 10/3.0 ))
The precision and range of shell builtin
$(( <expr> ))
depends on the platform. On a 32-bit platform it's limited to 32-bit integers (unlikeexpr
, which appears to support larger integers in a way that appears to be independent of platform).(N.B - beware that while
^
is a valid operator in most shells, it does not mean 'to the power of', but rather XOR.)Also - some shells are more forthcoming in this regard. Zsh, for example, allows floating point operations, such as
$(( 10/3.0 ))