shell编程——条件表达式和if判断

1、条件表达式

比较项test命令或者[][[]]:两个中括号
共同点都用于判断,不支持正则表达式用于判断,支持正则表达式
区别1、与或非:-a、-o、!
2、不支持正则表达式
1、与或非:&&、||、!
2、支持正则表达式
应用场景用于条件判断用于条件判断
  • 要查看test命令的判断条件可以查询man手册:man test

2、if判断

2.1、if分支结构

if [ 条件表达式 ];then
	条件成立要执行的命令
elif [ 条件表达式 ];then
	条件成立要执行的命令
else
	条件成立要执行的命令
fi

2.2、示例代码

if [ -d /mnt/hgfs/share_file_Ubuntu/shell ];then
	echo "目录存在"
fi

if [ -d /mnt/hgfs/share_file_Ubuntu/shell ];then
	echo "目录存在"
else
	echo "目录不存在"
fi

if [ -d /mnt/hgfs/share_file_Ubuntu/777 ];then
	echo "11111"
elif [ -d /mnt/hgfs/share_file_Ubuntu/shell ];then
	echo "2222"
else
	echo "3333"
fi

3、文件判断选项

FILE1 -ef FILE2
	FILE1 and FILE2 have the same device and inode numbers

FILE1 -nt FILE2
	FILE1 is newer (modification date) than FILE2

FILE1 -ot FILE2
	FILE1 is older than FILE2

-b FILE
	FILE exists and is block special

-c FILE
	FILE exists and is character special

-d FILE
	FILE exists and is a directory

-e FILE
	FILE exists

-f FILE
	FILE exists and is a regular file

-g FILE
	FILE exists and is set-group-ID

-G FILE
	FILE exists and is owned by the effective group ID

-h FILE
	FILE exists and is a symbolic link (same as -L)

-k FILE
	FILE exists and has its sticky bit set

-L FILE
	FILE exists and is a symbolic link (same as -h)

-O FILE
	FILE exists and is owned by the effective user ID

-p FILE
	FILE exists and is a named pipe

-r FILE
	FILE exists and read permission is granted

-s FILE
	FILE exists and has a size greater than zero

-S FILE
	FILE exists and is a socket

-t FD  
	file descriptor FD is opened on a terminal

-u FILE
	FILE exists and its set-user-ID bit is set

-w FILE
	FILE exists and write permission is granted

-x FILE
	FILE exists and execute (or search) permission is granted

4、字符串判断选项

-n STRING
	  the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
	  the length of STRING is zero

STRING1 = STRING2
	  the strings are equal

STRING1 != STRING2
	  the strings are not equal

5、整数判断选项

INTEGER1 -eq INTEGER2
	  INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
	  INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
	  INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
	  INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
	  INTEGER1 is less than INTEGER2

INTEGER1 -ne INTEGER2
	  INTEGER1 is not equal to INTEGER2

6、示例代码

#!/bin/bash

#判断文件
if [ -d /mnt/hgfs/share_file_Ubuntu/shell ];then
	echo "目录存在"
else
	echo "目录不存在"
fi

#判断字符串
string1="abc"
string2=

if [ "string1" != "string2" ]; then
	echo "string1 != string2"
fi

if [ "string1" ]; then
	echo "string1 is nonzero"
fi

#判断数字
num1=44
num2=55

if [ $num1 -lt $num2 ]; then
	echo "num1 < num2"
else
	echo "num1 >= num2"
fi

#双中括号,使用整数判断可以直接使用大于号、等于号、小于号
if [[ $num1 < $num2 ]]; then
	echo "num1 < num2"
else
	echo "num1 >= num2"
fi

#双中括号可以使用正则表达式
if [[ $num1 =~ [0-9] ]];then
	echo "num1包含数字"
fi

#实现判断条件的与操作
if [ -n "string1" ] && [ -n "string2" ];then
	echo "12345"
fi

7、运行结果

rlk@rlk:shell$ ./test.sh 
目录存在
string1 != string2
string1 is nonzero
num1 < num2
num1 < num2
num1包含数字
12345
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

正在起飞的蜗牛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值