本文共 3016 字,大约阅读时间需要 10 分钟。
-- Start
#!/bin/kshtypeset -i x=2;typeset -i y=3;typeset -i r=0;# 注意,算术表达式需要包含在$(())中,否则成了文本表达式# 注意,括号中没有 $ 哦r=$((x+y)); #加print "x+y=$r";r=$((x-y)); #减print "x-y=$r";r=$((x*y)); #乘print "x*y=$r";r=$((x/y)); #除print "x/y=$r";r=$((x**y)); #幂,相当于2的3次方print "x**y=$r";r=$((x%y)); #余print "x%y=$r";
比较数字有两种方法。
#!/bin/kshtypeset -i x=20;typeset -i y=3;# 注意,数字比较时需要包含在 (())中# 注意,括号中没有 $ 哦# 大于if ((x > y)); then print "#** $x > $y **#"fiif [[ $x -gt $y ]]; then print "#** $x gt $y **#"fi# 大于等于if ((x >= y)); then print "#** $x >= $y **#"fiif [[ $x -ge $y ]]; then print "#** $x ge $y **#"fi# 小于if ((x < y)); then print "#** $x < $y **#"fiif [[ $x -lt $y ]]; then print "#** $x lt $y **#"fi# 小于等于if ((x <= y)); then print "#** $x <= $y **#"fiif [[ $x -le $y ]]; then print "#** $x le $y **#"fi# 等于if ((x == y)); then print "#** $x == $y **#"fiif [[ $x -eq $y ]]; then print "#** $x le $y **#"fi# 不等于if ((x != y)); then print "#** $x != $y **#"fiif [[ $x -ne $y ]]; then print "#** $x ne $y **#"fi
#!/bin/ksh# ksh 支持 [] 和 [[]] 测试条件表达式,注意,它们有一些区别,推荐使用 [[]]# 变量是否包含在双引号中也有一些区别,推荐不要使用双引号typeset x='a';typeset y='b';# 判断字符串不为空if [[ $x ]]; then print "#** $x is not empty **#"fi# 判断字符串不为空,长度不为0if [[ -n $x ]]; then print "#** $x is not empty **#"fi# 判断字符串为空.长度为0.if [[ -z $x ]];then print "#** $x is empty **#"fi# 等于 -- 精确匹配if [ $x = a* ]; then print "#** 1 $x = a* **#"fiif [ $x == a* ]; then print "#** 2 $x == a* **#"fi# 等于 -- 精确匹配if [ "$x" = "a*" ]; then print "#** 3 \"$x\" = \"a*\" **#"fiif [ "$x" == "a*" ]; then print "#** 4 \"$x\" == \"a*\" **#"fi# 等于 -- 精确匹配if [[ "$x" = "a*" ]]; then print "#** 5 \"$x\" = \"a*\" **#"fiif [[ "$x" == "a*" ]]; then print "#** 6 \"$x\" == \"a*\" **#"fi# 等于 -- 匹配模式if [[ $x = a* ]]; then print "#** 7 $x start with a* **#"fiif [[ $x == a* ]]; then print "#** 8 $x start with a* **#"fi# 不等于 -- 精确匹配if [ $x != a* ]; then print "#** 1 $x != a* **#"fiif [ "$x" != "a*" ]; then print "#** 2 \"$x\" != \"a*\" **#"fiif [[ "$x" != "a*" ]]; then print "#** 3 \"$x\" != \"a*\" **#"fi# 不等于 -- 精确模式if [[ $x != a* ]]; then print "#** 4 $x != a* **#"fi# 大于,注意:字符串没有大于等于操作符if [[ $x > $y ]]; then print "#** $x > $y **#"fi# 小于,注意:字符串没有小于等于操作符if [[ $x < $y ]]; then print "#** $x < $y **#"fi
#!/bin/kshtypeset x='a';typeset y='b';typeset z='c';# 与if [[ $x < $y && $y < $z ]]; then print "#** $x < $y < $z **#"fi# 或if [[ $x < $y || $y < $z ]]; then print "#** $x < $y || $y < $z **#"fi# 非if [[ ! $x > $y ]]; then print "#** $x <= $y **#"fi
#!/bin/ksh# 按位与 &# 按位或 |# 按位非 ~# 按位异或 ^# 左移(相当于乘2) <<# 右移(相当于除2) >>
#!/bin/ksh# =# +=# -=# *=# /=# %=# &=# ^=# <<= # >>=typeset -i x=2;typeset -i r=0;# (()) 用来计算数学表达式((r+=x));print "r=$r";
#!/bin/kshtypeset -i x=1;# 自增运算符((x++));((++x));# 自减运算符((x--));((--x));
#!/bin/kshtypeset -i x=1;# 逗号表达式((x++,++x));print "x=$x";
#!/bin/kshtypeset -i x=2;typeset -i y=3;typeset -i r=0;((r=(y > x) ? y : x));print "r=$r";
-- 更多参见:
-- 声 明:转载请注明出处
-- Last Updated on 2015-10-03
-- Written by ShangBo on 2015-09-23 -- End