linux双括号语法及双方括号语法

一般我们使用如下语法

if [ expression ] 
then

if

linux中提供了高级的双括号语法。
在之前,如果要判断数字大小,大于号需要转义,否则会被认为重定向
举例:

[root@localhost shell]# cat test1.sh 
#!/bin/bash
num=10
if [ num > 8 ];then
	echo "10>8"
fi

程序解释:上面程序执行,虽然输出了10>8,但不是真正意义的比大小,而是认为了重定向,生成了文件名8。

[root@localhost shell]# ./test1.sh 
10>8
[root@localhost shell]# ls
8  test1.sh

以上通过转义即可解决问题。

[root@localhost shell]# cat test1.sh 
#!/bin/bash
num=10
if [ num \> 8 ];then
	echo "10>8"
fi

双括号语法

[root@localhost shell]# cat test1.sh 
#!/bin/bash
num=10
if (( $num ** 2 > 90 ));then
	(( result = $num ** 2 ))
	echo "10的平方="$result
fi
[root@localhost shell]# ./test1.sh 
10的平方=100

大于号不需要进行转义,写起来也简单些。

双方括号语法

双方括号里的expression使用了test命令中采用的标准字符串比较。但它提供了test命令未提供的另一个特性,模式匹配。

[root@localhost shell]# cat test1.sh 
#!/bin/bash
if [[ $USER == r* ]]
then
	echo "hello $USER"
else
	echo "Sorry,you not start with r"
fi
[root@localhost shell]# ./test1.sh 
hello root

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值