linux+expr+变量,expr(将参数作为表达式求值)_语法_示例_Unix&Linux命令_uc电脑园官网...

expr text : '.*'

Performs a regular expression match. The regular expression after the colon is matched to the text before the colon. The returned output is the number of characters that matched. Here, the regular expression '.*' represents "any number of any character", therefore the result is:

4

expr text : tex

Returns the number of characters from the regular expression after the colon which appear in the text before the colon. Here, the regular expression 'tex' represents "exactly the consecutive characters t, e, and x", so the output would be:

3

expr text : '\(.*\)'

Here, the regular expression '\(.*\)' represents "The actual text (whatever appears in between the parentheses, which are escaped with backslashes) which matches the pattern .*, which itself represents any number of any character." Matched against the text text, this returns the string exactly:

text

expr 5 = 5

Returns 1 (true) if the expressions are equivalent, or 0 (false) if they are not. Here, the values 5 and 5 are equal, and therefore equivalent, so the output will be:

1

expr '5' = '5'

Here, two strings are being compared for equivalence. If the strings match exactly, character-for-character, the result will be 1 (true). Otherwise, the result will be 0. Here, the result is:

1

expr 5 \> 10

Here, the result is 1 (true) if 5 is less than 10, otherwise the result is 0. The "less than" symbol ("

0

expr 5 \!= 5

Just as the = operator tests for equivalence, the != operator tests for non-equivalence. If the two values being tested are not equivalent, the result is true (1), otherwise the result is false (0). 5 is equivalent to 5, so the result is false:

0

expr 5 \!= "5"

"Equivalence" is not the same as "equality". 5 is a number, and "5" is a string, so technically they are not "equal," but expr considers them equivalent because it reads the string for its contents, sees that it is a number, and uses the value of that number in the comparison. So the value 5 is equivalent to a string containing the number "5". Therefore the answer here is false; they are not non-equivalent:

0

This next example will show, in a series of commands, how expr can be used to increment the value of a variable.

If we define a variable named count, setting it to zero:

count=0

...we can output the value of that variable with the echo command:

echo $count

0

...now we can increment it by setting it to the value of an expr evaluation, which returns the value of the variable, plus one:

count=`expr $count + 1`

...and we can check the updated value with another echo:

echo $count

1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值