Linux Shell 编程中的条件判断

7 篇文章 0 订阅

  • 在实际使用中,我们需要对脚本中的逻辑进行控制。
  • 在面向过程的编程语言中,通常进行逻辑流程控制的结构就是条件循环
  • 在 Shell 中,提供了两种结构进行条件的判断
    • if — then
    • case

1 if — then 结构;

  • 单条件
# 语法结构:
# command 是 Shell 中的命令,
# 会在 command 的返回码为 0 的时候判定条件成立,会执行 then 后面的语句
if command		
then			# then 后面执行当 command 条件成立时的语句
	commands
fi				# fi 结尾

# 举例:
vim ctest1
# 写入以下内容
#! /bin/bash

if date
then
	echo "command worked"
fi 

# 保存后退出,给与执行权限
chmod u+x ctest1
# 执行脚本
./ctest1
# 输出
2019年 07月 24日 星期三 17:16:22 CST	# date 命令输出当前时间
command worked		# 条件判断成立(date 命令成功执行),返回输出

# 修改 ctest1
vim ctest1
# 写入以下内容
#! /bin/bash

if date121212121212
then
	echo "command worked"
fi
echo "outside if"

# 保存后退出,执行脚本
./ctest1
# 输出
/data/shellscript/ctest1:行4: date121212121212: 未找到命令
outside if
  • 全覆盖
# 语法结构:
if command		
then			# then 后面执行当 command 条件成立时的语句
	commands
else			# 判断不成功时,执行 else 后面的命令
	commands	
fi

# 修改 ctest1
vim ctest1
# 写入以下内容
#! /bin/bash

if date121212121212
then
	echo "command worked"
else
	echo "command failed"
fi

# 保存后退出,执行脚本
./ctest1
# 返回
/data/shellscript/ctest1:行4: date121212121212: 未找到命令
command failed
  • 条件嵌套
# 语法结构:
if command1
then
	if command2
	then			
		commands
	fi			
	commands	
fi

# 为了程序美观,将嵌套的 if 语句扁平化
if command1
then
	commands
elif command2
then
	commands
else
	commands
fi

# 以上的 if 条件语句都是以 command (if 后执行的命令的退出码)来进行判断
# 但是实际进行逻辑判断时,需要使用很多的条件语句
# 在 Shell 中实现条件判断
# test 命令: 会在 test 后面的条件成立时返回退出码 0,在失败时返回退出码 1
# 也就是说,if 语句本身并不支持条件的表达式,而依旧是通过命令的退出码来进行条件的判断
# 只不过在使用条件判断时,可以使用 test 命令来实现对条件的判断
# test 命令在 Shell 中提供了 2 种模式,一种是如下显示
if test condition
then
	commands
fi

# 另一种是使用了中括号来代替 test,也就是说,在中括号“[]”中写入条件判断语句
# 这种写法和之前在进行变量的赋值和计算非常相似
if [ condition ]
then
	commands
fi

# 或者
if [ condition ];then
    commands
fi

# if 也支持负荷条件判断
[condition1] && [condition2]
[condition1] || [condition2]

# test 命令支持的判断条件:1.数值比较 2.字符串比较 3.文件比较
# 可以利用 test 命令通过 if 语句实现数值条件、字符串条件、文件条件的比较判断
# 举例:
vim ctest2
# 写入以下内容
#! /bin/bash

echo "please input a number:"
read num

if [ $num -gt 10 ]
then
	echo "the number is bigger than 10"
fi

if [ $num -lt 10 ] && [ $num -gt 5 ]
then
	echo "the number is between 5 and 10"
fi

# 保存退出,并加上执行权限
chmod u+x ctest2
# 执行
please input a number:
6 
the number is between 5 and 10

2. 常用判断条件;

  • 数值比较(使用字母操作符)
数值比较
相等-eq
大于等于-ge
大于-gt
小于等于-le
小于-lt
不等于-ne
  • 字符串比较(使用数学符号)
    • 字符串大于和小于是根据字符串中使用字符的 ASCII 码的顺序进行判断的
字符串比较
str1 = str2相等
str1 != str2不等
str1 > str2大于
str1 < str2小于
-n str1字符串长度是否非 0
-z str1字符串长度是否为 0
# 举例:
vim ctest3
# 写入以下内容
#! /bin/bash

str1='abcd'
str2='bacd'

if [ $str1 \< $str2 ]
then
	echo 'str1 is less than str2'
elif [ $str1 /> $str2 ]
then
	echo 'str1 is bigger than str2'
elif [ $str1 = $str2 ]
then
	echo 'str1 is equal than str2'
fi

# 保存退出,并加上执行权限
chmod u+x ctest3
# 执行
./ctest3		# 输出 str1 is less than str2
  • 文件比较
文件比较
-d file检查 file 是否存在并是一个目录
-e file检查 file 是否存在
-f file检查 file 是否存在并是一个文件
-s file检查 file 是否存在并非空
file1 -nt file2检查 file1 是否比 file2 新
file1 -ot file2检查 file1 是否比 file2 旧
-r file检查 file 是否存在并可读
-w file检查 file 是否存在并可写
-x file检查 file 是否存在并可执行
-O file检查 file 是否存在并属于当前用户所有
-G file检查 file 是否存在并且默认组与当前用户相同
# 举例:
vim ctest4
# 写入以下内容
#! /bin/bash

shellscript='ctest3'

if [ -d $shellscript ]
then 
	echo " $shellscript is a directory"
fi

if [ -f $shellscript ]
then
	echo " $shellscript is a file"
fi

if [ -x $shellscript ]
then
	echo " $shellscript is an execute file"
fi

# 保存退出,并加上执行权限
chmod u+x ctest4
# 执行
./ctest4
# 返回
ctest3 is a file
ctest3 is an execute file
  • 高级判断
# 高级数学表达式
(( expression ))

# 高级字符串比较
[[ expression ]]

# 举例(高级数学表达式):
vim ctest5
# 写入以下内容
#! /bin/bash

val1=10

# 在这里使用数值比较,
# 并没有像刚才使用 test 命令时使用转义字符的方式,而是直接使用大于号
if(( $val1 ** 2 > 90 ))
then
	# 这里使用“(())”进行一个数学运算,
	# 不但可以将它作为一个判断的条件,也可以将它变成一个独立的命令执行
	(( val2 = $val1 **2))
	# 双引号中引用变量,会解析为变量的值
	# 为 val2 的值赋了一个 val1 的平方值
	echo "The square of $val1 is $val2"
fi

# 保存退出,并加上执行权限
chmod u+x ctest5
# 执行
./ctest5		# 输出 The square of 10 is 100

# 举例(高级字符串比较):
vim ctest6
# 写入以下内容
#! /bin/bash

if [[ $USER == h* ]]
then
	echo "hello $USER"
fi

# 保存退出,并加上执行权限
chmod u+x ctest6
# 执行
./ctest6		# 输出 hello hualaoshuan

3. case 语句。

  • case 和 if 不同,case 中可以同时判断多种条件
  • Shell 中的 case 语法就类似于 C 语言中的 switch—case。在 C 语言中,每一种条件都是在 switch 中的 case 中来实现的。
  • 在 Shell 中,使用 case 语法,这里的 case 就相当于 C 语言中的 switch。这种语法类似于数据库查询中的 sql 语言中的 case
# 语法结构:
case variable in
pattern1 | pattern2) commands1 ;;	# 在 pattern1 或者 pattern2 这两种条件任何一种条件成立时会执行的命令
pattern3) commands3 ;;				# 两个分号作为结束标志
*) default commands ;;				# “*”匹配所有条件。case 是从上到下匹配条件的,星号表示上面没匹配到的条件都在这里执行
esac								# 结束标志,和 case 字母顺序相反

# 举例:
vim ctest7
# 写入以下内容
#! /bin/bash

echo "please input number of month"
read num

case $num in
1)
	echo "the month is Jan.";;
2)
	echo "the month is Febr.";;
3)
	echo "the month is Mar.";;
4)
	echo "the month is Apr.";;
*)
	echo "wrong number";;
esac

# 保存退出,并加上执行权限
chmod u+x ctest7
# 执行
./ctest7
please input number of month
3
the month is Mar.

## 由于 case 中的条件是从上到下执行的,
## 那么在写默认值时,必须将“*”放在最后,否则后面的命令就不会被执行

# 字符串判断
# 举例:
vim ctest8
# 写入以下内容
#! /bin/bash

echo "please input Yes | No:"
read var

case $var in
yes | Yes | YES | Y | y)
	echo "You said yes"
	echo "OK!";;
no)
	echo "You said no";;
*)
	echo "wrong saying";;
esac

# 保存退出,并加上执行权限
chmod u+x ctest8
# 执行
./ctest8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值