shell条件测试

一.命令执行结果判定

&& 在命令执行后如果没有任何报错时会执行符号后面的动作

|| 在命令执行后如果命令有报错会执行符号后的动作

示例:

复制代码
[root@qingdeng shell3]# sh sl.sh
/mnt/file is not exist
no

. 条件判断方法

在 shell 程序中,用户可以使用测试语句来测试指定的条件表达式的条件的真或假

复制代码
[root@qingdeng shell3]# a=1
[root@qingdeng shell3]# b=1
[root@qingdeng shell3]# test $a = $b && echo yes || echo no
yes

[root@qingdeng shell3]# a=1
[root@qingdeng shell3]# b=2
[root@qingdeng shell3]# [ "$a" = "$b" ] && echo yes || echo no~
no~

[root@qingdeng shell3]# a=2
[root@qingdeng shell3]# [[ $a =~ 2|10 ]] && echo yes || echo no
yes

[root@qingdeng shell3]# a=3
[root@qingdeng shell3]# (( $a>0,$a<10 )) && echo yes
yes

. 判断表达式

3.1. 文件判断表达式

a )判断文件时否存在

复制代码
[root@qingdeng shell3]# [ -a "file" ] && echo yes || echo no
no
[root@qingdeng shell3]# touch file
[root@qingdeng shell3]# [ -a "file" ] && echo yes || echo no
yes

b )判断文件类型

复制代码
[root@qingdeng shell3]# [ -d /mnt ] && echo /mnt is directory
/mnt is directory

c )判定文件是否存在并不为空

复制代码
[root@qingdeng shell3]# [ -s file ] || echo file is not exist or file is empty file
file is not exist or file is empty file
[root@qingdeng shell3]# ll file
-rw-r--r-- 1 root root 0 12月  8 20:53 file

d )判定文件权限

复制代码
[root@qingdeng shell3]# [ -x file ] || echo file is not execute
file is not execute

e )判定文件新旧

复制代码
[root@qingdeng shell3]# touch file1
[root@qingdeng shell3]# [ file -ot file1 ] && echo yes
yes

3.2. 字符串测试表达式

  1. 检测变量是否未空

    [root@qingdeng shell3]# b=hello
    [root@qingdeng shell3]# [ -z "b" ] && echo yes [root@qingdeng shell3]# [ -z "b" ] && echo yes || echo no
    no
    [root@qingdeng shell3]# [ -n "$b" ] && echo yes || echo no
    yes

  2. 比较字符

    [root@qingdeng shell3]# a=quite
    [root@qingdeng shell3]# [ "$a" = "b" ] && echo yes || echo no
    no

. 练习脚本

要求

++[root@haha ~]# sh checkfile.sh
please input filename: 当未输入任何字符时回车
Error: no checkfilename,Please input filename for check !! 报错
please input filename: /mnt/leefile 当输入/mnt/leeifle时
文件不存在时
/mnt/file is not exist
当文件存在时输入文件类型
/mnt/file is commom file 注意文件是什么类型就输出文件类型的相关输出
++

复制代码
[root@qingdeng shell]# sh checkfile.sh
please input filename: 

Error: no checkfilename,Please input filename for check !!
[root@qingdeng shell]# sh checkfile.sh
please input filename: 
file
file is not exist
[root@qingdeng shell]# sh checkfile.sh
please input filename: 
/mnt/file
/mnt/file is commom file

. 整数测试表达式

复制代码
[root@qingdeng shell3]# a=1
[root@qingdeng shell3]# b=2
[root@qingdeng shell3]# [ $a -eq $b ] && echo yes || echo no
no
[root@qingdeng shell3]# [ $a -ne $b ] && echo yes || echo no
yes
[root@qingdeng shell3]# [ $a -ge $b ] && echo yes || echo no
no
[root@qingdeng shell3]# [ $a -gt $b ] && echo yes || echo no
no
[root@qingdeng shell3]# [ $a -le $b ] && echo yes || echo no
yes
[root@qingdeng shell3]# [ $a -lt $b ] && echo yes || echo no
yes

. 逻辑操作符

复制代码
[root@qingdeng shell3]# a=5
[root@qingdeng shell3]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes
yes
[root@qingdeng shell3]# [ "$a" -gt "0" ] && echo yes
yes
[root@qingdeng shell3]# [ "$a" -lt "0"   -o "$a" -gt "10" ] && echo yes
[root@qingdeng shell3]# [ "$a" -lt "0"   -o "$a" -gt "10" ] || echo yes
yes

. 判定综合训练

  1. 请用判定的方式书写一个 1 分 10 秒的倒计时,确保倒计时器符合日常显示规则
  1. 请书写一个猜数游戏,要求如下
复制代码
[root@qingdeng shell]# sh guest_number.sh
please input a number between 0 ~ 9:
Error:Please input a number between 0 - 9
please input a number between 0 ~ 9:4
  4 is too small
please input a number between 0 ~ 9:6
  6 is too big
please input a number between 0 ~ 9:5
 HAHAHA!!! 
 YES You are right !!
Please again?(y/n)y
please input a number between 0 ~ 9:4
  4 is too big
please input a number between 0 ~ 9:3
  3 is too big
please input a number between 0 ~ 9:2
  2 is too big
please input a number between 0 ~ 9:1
  1 is too big
please input a number between 0 ~ 9:0
 HAHAHA!!! 
 YES You are right !!
Please again?(y/n)y
please input a number between 0 ~ 9:44
Error: is not between 0 ~ 9,Please input a number between 0 - 9
please input a number between 0 ~ 9:
相关推荐
树℡独3 小时前
ns-3仿真之应用层(五)
服务器·网络·tcp/ip·ns3
小小管写大大码3 小时前
如何让vscode变得更智能?vscode接入claude实现自动编程
运维·ide·vscode·自动化·编辑器·ai编程·腾讯云ai代码助手
嵩山小老虎3 小时前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
Fleshy数模4 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
zhang133830890754 小时前
CG-09H 超声波风速风向传感器 加热型 ABS材质 重量轻 没有机械部件
大数据·运维·网络·人工智能·自动化
a41324474 小时前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
Configure-Handler4 小时前
buildroot System configuration
java·服务器·数据库
津津有味道4 小时前
易语言TCP服务端接收刷卡数据并向客户端读卡器发送指令
服务器·网络协议·tcp·易语言
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.5 小时前
Keepalived VIP迁移邮件告警配置指南
运维·服务器·笔记
物联网软硬件开发-轨物科技5 小时前
【轨物洞见】告别“被动维修”!预测性运维如何重塑老旧电站的资产价值?
运维·人工智能