📄 ex45.sh
字号:
#!/bin/bash# Demonstrating some of the uses of 'expr'# =======================================echo# Arithmetic Operators# ---------- ---------echo "Arithmetic Operators"echoa=`expr 5 + 3`echo "5 + 3 = $a"a=`expr $a + 1`echoecho "a + 1 = $a"echo "(incrementing a variable)"a=`expr 5 % 3`# moduloechoecho "5 mod 3 = $a"echoecho# Logical Operators# ------- ---------# Returns 1 if true, 0 if false,#+ opposite of normal Bash convention.echo "Logical Operators"echox=24y=25b=`expr $x = $y` # Test equality.echo "b = $b" # 0 ( $x -ne $y )echoa=3b=`expr $a \> 10`echo 'b=`expr $a \> 10`, therefore...'echo "If a > 10, b = 0 (false)"echo "b = $b" # 0 ( 3 ! -gt 10 )echob=`expr $a \< 10`echo "If a < 10, b = 1 (true)"echo "b = $b" # 1 ( 3 -lt 10 )echo# Note escaping of operators.b=`expr $a \<= 3`echo "If a <= 3, b = 1 (true)"echo "b = $b" # 1 ( 3 -le 3 )# There is also a "\>=" operator (greater than or equal to).echoecho# String Operators# ------ ---------echo "String Operators"echoa=1234zipper43231echo "The string being operated upon is \"$a\"."# length: length of stringb=`expr length $a`echo "Length of \"$a\" is $b."# index: position of first character in substring# that matches a character in stringb=`expr index $a 23`echo "Numerical position of first \"2\" in \"$a\" is \"$b\"."# substr: extract substring, starting position & length specifiedb=`expr substr $a 2 6`echo "Substring of \"$a\", starting at position 2,\and 6 chars long is \"$b\"."# The default behavior of the 'match' operations is to#+ search for the specified match at the ***beginning*** of the string.## uses Regular Expressionsb=`expr match "$a" '[0-9]*'` # Numerical count.echo Number of digits at the beginning of \"$a\" is $b.b=`expr match "$a" '\([0-9]*\)'` # Note that escaped parentheses# == == + trigger substring match.echo "The digits at the beginning of \"$a\" are \"$b\"."echoexit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -