For faster navigation, this Iframe is preloading the Wikiwand page for expr.

expr

此条目也许具备关注度,但需要可靠的来源来加以彰显。(2021年7月26日)请协助补充可靠来源改善这篇条目

expr
原作者Dick Haight
(贝尔实验室)
开发者多个开源软件商业软件开发者
首次发布1979年,​45年前​(1979
操作系统Unix, Unix-like, IBM i
平台跨平台
类型命令

exprUnixUnix-like操作系统下计算表达式及输出相应结果的命令行工具,最早见于Unix v7。这个命令也已经被移植到IBM i系统上。[1]

概览

[编辑]

expr计算整数字符串表达式,包括正则表达式格式匹配。表达式中的每一个符号(运算符、值等)必须以分开的参数传递。书写表达式最大的挑战就是要避免Unix shellexpr需要处理的字符当作控制指令而执行动作。

语法

[编辑]

语法: expr expression

三种运算可用:

  • 整数:加、减、乘、除、求余数
  • 字符串:匹配正则表达式;某些版本上还可以查找一组字符("index")、子字符串("substr"),求字符串长度("length")
  • 两者皆可:比较(如:相等、不等、小于)

示例

[编辑]

下面是一个涉及布尔表达式的(不符合POSIX标准)例子:

expr length  "abcdef"  "<"  5  "|"  15  -  4  ">"  8

这个例子会输出"1"。这是因为"abcdef"长为6(也就是大于5),故"|"左侧表达式返回0;但“15-4”得11(也就是大于8),因此右侧表达式为真,使得整个表达式为真,所以结果为1。在此示例中,程序退出状态为0。

在纯算术运算下,通常使用bc更为简便,是因为它将整个表达式看作一个参数。例如:

echo "3 * 4 + 14 / 2" | bc

可移植脚本编程时,"index"、"length"、"match"和"substr"等命令的使用需要避免;字符串匹配仍然可行,但必须使用"string : regexp"语法。

另请参见

[编辑]

参考资料

[编辑]
  1. ^ IBM. IBM System i Version 7.2 Programming Qshell (PDF). [2020-09-05]. (原始内容存档 (PDF)于2020-09-18) (英语). 

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
expr
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?