首页 > 系统相关 >Shell编程之条件判断语句

Shell编程之条件判断语句

时间:2024-07-08 14:33:16浏览次数:10  
标签:语句 11 shell 20 centos 编程 VM Shell root

Shell编程之条件判断语句

一、条件判断

Shell环境根据命令执行后的返回状态值(echo $?)来判断是否执行成功,当返回值为0表示成功或正确,返回值为非0值表示失败或异常。(补充:Linux判断依据在别的编程语言中是反过来的,如java假为0,真为1)

1、test命令

有两种方式

  • test 条件表达式
  • [ 条件表达式 ] [ ] 相当于test 中括号之间需要有空格

案例:0表示成立 其他值表示不成立

[root@VM-20-11-centos shell]# test 1=1
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

2、文件测试

  • test 选项 文件/目录路径 test 选项 $变量
  • [ 选项 文件/目录路径 ] [ 选项 $变量 ]

常用的操作符号有

-e 测试目录或文件是否存在(Exist)
-d 测试是否为目录(Directory)
-f 测试是否为文件(File)
-r 测试当前用户是否有权限读取(Read)
-w 测试当前用户是否有权限写入(Write)
-x 测试是否设置有可执行(Excute)权限。
-L 测试是否为符号链接

案例:

# 返回值为1代表不存在
[root@VM-20-11-centos shell]# [ -e wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为1代表不是目录
[root@VM-20-11-centos shell]# [ -d wss ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]#
# 返回值为0代表是文件
[root@VM-20-11-centos shell]# ls -alh
total 28K
drwxrwxrwx 2 root root 4.0K Jul  8 10:10 .
drwxr-xr-x 5 root root 4.0K Jul  5 10:46 ..
-rw-r--r-x 1 root root  341 Jul  8 09:47 auto_var.sh
-rw-r--r-x 1 root root   54 Jul  5 17:44 first_shell.sh
-rw-r--r-x 1 root root  146 Jul  8 10:03 test_dir.sh
-rw-r--r-x 1 root root   85 Jul  8 09:59 test_num.sh
-rw-r--r-x 1 root root  200 Jul  8 10:11 test_scores.sh
[root@VM-20-11-centos shell]# [ -f auto_var.sh ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

3、整数值比较

  • 格式:[ 整数变量1 操作符 整数变量2 ]

常用的操作符

-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-ge 大于等于

案例

[root@VM-20-11-centos shell]# [ 1 -le 1 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

4、字符串判断

格式

  • test "字符串1" == "字符串2"
  • [ "字符串1" == "字符串2" ]

案例:

[[ "字符串1" =~ "字符串2 ]] # 判断字符串1是否包含字符串2

test -z "字符串" test -z "$变量" [ -z "$变量" ] #判断字符串或变量是否为空

test -n "字符串" test -n "$变量" [ -n "$变量" ] #判断字符串或变量是否有字符串


[root@VM-20-11-centos shell]# [ "abc" == "123" ]
[root@VM-20-11-centos shell]# echo $?
1

[root@VM-20-11-centos shell]# [[ "abcdfre" =~ "bc" ]]
[root@VM-20-11-centos shell]# echo $?
0

[root@VM-20-11-centos shell]# [ -z "a" ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ -z "" ]
[root@VM-20-11-centos shell]# echo $?
0

[root@VM-20-11-centos shell]# [ -n ""]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ -n null ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

5、逻辑测试

  • 格式1:[ 表达式1 ] 操作符 [ 表达式2 ]
  • 格式2:命令1 操作符 命令2

常用操作符 -a或者&& 而且的意思

             -o或者 ll  或者的意思

              !: 代表取反

    [ 表达式1 ] && [ 表达式2 ] ;[ 表达式1 -a 表达式2 ] ;[[ 表达式1 && 表达式2 ]]
[root@VM-20-11-centos shell]# [ 1=1 ] && [ 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -a 2=2 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [[ 1=1 && 2=2 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
[root@VM-20-11-centos shell]# [[ 1=1 || 2=3 ]]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]# [ 1=1 -o "abc"=="bdc" ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#
# 取反
[root@VM-20-11-centos shell]# [ ! 12=12 ]
[root@VM-20-11-centos shell]# echo $?
1
[root@VM-20-11-centos shell]# [ 12=12 ]
[root@VM-20-11-centos shell]# echo $?
0
[root@VM-20-11-centos shell]#

案例2

#!/bin/bash
str="yingtan"
if [[ $str == "yingtan" || $str == "鹰潭" ]];then 
    echo "鹰潭"
else
    echo "不是鹰潭"
fi

执行结果

[root@VM-20-11-centos shell]# sh test_str.sh
鹰潭
[root@VM-20-11-centos shell]#

标签:语句,11,shell,20,centos,编程,VM,Shell,root
From: https://www.cnblogs.com/wusongsong/p/18289799/shellbian-cheng-zhi-tiao-jian-pan-duan-yu-ju

相关文章

  • Shell编程之条件判断语句
    Shell编程之条件判断语句一、条件判断Shell环境根据命令执行后的返回状态值(echo$?)来判断是否执行成功,当返回值为0表示成功或正确,返回值为非0值表示失败或异常。(补充:Linux判断依据在别的编程语言中是反过来的,如java假为0,真为1)1、test命令有两种方式test条件表达式[条件表......
  • Shell - $0、$1、$2、$?、$!、$$、$*、$#、$@的含义
    $0、$1、$2的含义?在Shell中,$1代表传递给Shell脚本或函数的第一个命令行参数。这些参数也被称为位置参数。例如,如果你运行一个脚本并传递了两个参数,如./script.shfilename1dir1那么$0将是脚本名称本身(在这个例子中是‘......
  • Shell - 2>&1 是什么意思
    2>&1的具体含义是什么?问:为了将stderr和stdout合并成stdout流,我们将它附加到命令中:2>&1例如,要查看编译g++main.cpp的前几个错误:g++main.cpp2>&1|head2>&1的具体含义是什么?答:文件描述符1是标准输出(stdout)。文件描述符2是标准错......
  • Python中的面向对象编程:从入门到实践
    Python中的面向对象编程:从入门到实践一、引言面向对象编程(Object-OrientedProgramming,OOP)是Python语言中一个核心概念,它提供了一种处理程序复杂性的方法。通过OOP,我们可以使用对象和类的概念来模拟现实世界的实体和行为。本文将深入探讨Python中的面向对象编程,包括类与对......
  • 2、需求工具 - 编程实战工具系列文章
          需求分析工具       笔者对于需求分析工具的应用,主要是思维导图软件和Word。思维导图用来理清需要的需求功能,而Word用来记录每个需求功能的实际内容。      对于思维导图软件,笔者用过几个,但是有些需要注册码,有些需要费用,这些都是需要钱才能进行使......
  • 【linux/shell】如何创建脚本函数库并在其他脚本中调用?
    在Shell中创建和使用脚本库通常涉及以下几个步骤:1. 创建脚本库文件脚本库通常是包含了一系列可重用函数的Shell脚本文件。你可以使用文本编辑器创建一个脚本库文件,例如 mylib.sh 。nanomylib.sh在文件中,你可以定义一些函数,例如:#!/bin/bashfunctiongreet{  ech......
  • 随着云计算和容器技术的广泛应用,如何在这些环境中有效地运用 Shell 进行自动化部署和
    在云计算和容器技术的环境中,Shell脚本可以被用于自动化部署和管理任务。下面是一些在这些环境中有效使用Shell进行自动化部署和管理的方法:在云环境中,使用云服务提供商的API进行自动化管理。例如,使用命令行工具或SDK来管理云资源、启动虚拟机、创建存储等。将这些A......
  • AI与编程:一个学生的心路历程与思考
    前言大家好,本人是在一个在校的大学生,方向是前端语言。爱好是码代码和看一点小新闻,游戏也是喜爱的。其实本篇文章的想法是源于网上一些人对AI以及对前端的看法,看完网上的评论后我也是有感而发。本篇文章的讨论中心也是围绕着AI和前端,作为一个学生我的观察与思考,但是本人其......
  • 在Linux中,bash shell 中的 hash 命令有什么作用?
    在bashshell中,hash命令与命令查找和缓存机制紧密相关。当你输入一个命令时,bash需要找到该命令的可执行文件的位置(即其路径)以便执行它。bash有几种方式来完成这个任务,其中之一就是使用哈希表(hashtable)来缓存之前查找过的命令的路径。这就是hash命令发挥作用的地方。1.hash命令......
  • C语言大师之路:从零到王者/新手入门(3)选择语句
    序(一些闲话)我希望我的语言不要像专业书那样让人眼花缭乱,所以当我解释语法时,我会尽量避免使用太多专业术语,让说明更容易理解。我会用通俗易懂的语言来解释,而不是像专业书籍那样让人感到困惑。本人计划通过文章分享C语言的核心知识点和学习心得。鉴于仍处于学习阶段,文章中可......