博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ksh 运算符
阅读量:4049 次
发布时间:2019-05-25

本文共 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

你可能感兴趣的文章
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
模拟屏学习资料_什么是PAL制式
查看>>
模拟屏学习资料_模拟视频 入门
查看>>
西藏之旅
查看>>
Oracle中定时执行问题
查看>>
三时业
查看>>
佛教三宝-三皈依
查看>>
杂阿含经喻世间有四等马
查看>>
考研前夜涂笔
查看>>