ksh 运算符

本文介绍了ksh shell中的各种运算符,包括算术运算符、数字和字符串比较运算符、逻辑运算符、位运算符、赋值运算符、自增自减运算符、逗号运算符及条件运算符。详细内容请参阅ksh精萃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- Start

算术运算符

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值