ksh 运算符
2015-09-23 13:33
253 查看
-- Start
-- 更多参见:ksh 精萃
-- 声 明:转载请注明出处
-- Last Updated on 2015-10-03
-- Written by ShangBo on 2015-09-23
-- End
算术运算符
#!/bin/ksh typeset -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/ksh typeset -i x=20; typeset -i y=3; # 注意,数字比较时需要包含在 (())中 # 注意,括号中没有 $ 哦 # 大于 if ((x > y)); then print "#** $x > $y **#" fi if [[ $x -gt $y ]]; then print "#** $x gt $y **#" fi # 大于等于 if ((x >= y)); then print "#** $x >= $y **#" fi if [[ $x -ge $y ]]; then print "#** $x ge $y **#" fi # 小于 if ((x < y)); then print "#** $x < $y **#" fi if [[ $x -lt $y ]]; then print "#** $x lt $y **#" fi # 小于等于 if ((x <= y)); then print "#** $x <= $y **#" fi if [[ $x -le $y ]]; then print "#** $x le $y **#" fi # 等于 if ((x == y)); then print "#** $x == $y **#" fi if [[ $x -eq $y ]]; then print "#** $x le $y **#" fi # 不等于 if ((x != y)); then print "#** $x != $y **#" fi if [[ $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 # 判断字符串不为空,长度不为0 if [[ -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* **#" fi if [ $x == a* ]; then print "#** 2 $x == a* **#" fi # 等于 -- 精确匹配 if [ "$x" = "a*" ]; then print "#** 3 \"$x\" = \"a*\" **#" fi if [ "$x" == "a*" ]; then print "#** 4 \"$x\" == \"a*\" **#" fi # 等于 -- 精确匹配 if [[ "$x" = "a*" ]]; then print "#** 5 \"$x\" = \"a*\" **#" fi if [[ "$x" == "a*" ]]; then print "#** 6 \"$x\" == \"a*\" **#" fi # 等于 -- 匹配模式 if [[ $x = a* ]]; then print "#** 7 $x start with a* **#" fi if [[ $x == a* ]]; then print "#** 8 $x start with a* **#" fi # 不等于 -- 精确匹配 if [ $x != a* ]; then print "#** 1 $x != a* **#" fi if [ "$x" != "a*" ]; then print "#** 2 \"$x\" != \"a*\" **#" fi if [[ "$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/ksh typeset 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/ksh typeset -i x=1; # 自增运算符 ((x++)); ((++x)); # 自减运算符 ((x--)); ((--x));
逗号运算符
#!/bin/ksh typeset -i x=1; # 逗号表达式 ((x++,++x)); print "x=$x";
条件运算符
#!/bin/ksh typeset -i x=2; typeset -i y=3; typeset -i r=0; ((r=(y > x) ? y : x)); print "r=$r";
-- 更多参见:ksh 精萃
-- 声 明:转载请注明出处
-- Last Updated on 2015-10-03
-- Written by ShangBo on 2015-09-23
-- End
相关文章推荐
- cf 580B
- Git的使用和相关配置
- 数据入库总结
- leetcode - Missing Ranges
- 著名的英文搜索引擎
- 程序员迷茫的感情生活
- 登录时旋转等待效果
- 重载和多态之间的关系?!(C++初学者)
- MySQL DBA 面试全揭秘
- python3 入门 (二) 列表的使用
- 软件重构
- 跟马哥学linux (lesson 8)awk & sed功能总结
- no lable views point to this text field with an android:lablFor=”@+id/@+id
- HDU 1054 Strategic Game(简单树形DP)
- java文件切割工具
- 数独游戏设计与实现之第二篇——开发环境
- 导航栏控制器
- javascript调用EXE程序传值
- android gradle command
- IE版本的语句和placeholder——疑问1