shell结构化命令if-then-else语句

 更新时间:2024年11月01日 10:00:16   作者:pineapple rong  
在Shell脚本编程中,if-then-else语句提供了基于条件执行不同命令的能力,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在if-then语句中,不管命令是否成功执行,你都只有一种选择。如果命令返回一个非0退出状态码,则bash shell会继续执行脚本中的下一条命令。在这种情况下,如果能够执行另一组命令就好了。这正是if-then-else语句的作用。

if-then-else语句在语句中提供了另外一组命令:

if command
then 
       command
else
        command
fi

当if语句中的命令返回退出状态码0时,then部分中的命令会被执行,这跟普通的if-then语句一样。当if语句中的命令返回非0退出状态码时,bash shell会执行else部分中的命令。

Shell脚本首先判断文件test1是否可读,如果是,则输出 is readable !的提示信息;否则不进行任何动作。

[root@localhost 20190105]# vi test.sh
filename=test1
if [ -r $filename ]            //输出test1可读则输出信息
then
echo $filename' is readable !'
fi
[root@localhost 20190105]# sh test.sh
test1 is readable !

Shell脚本会判断number变量是否等于100,如果是,则输出 The number is equal 100 !的提示;否则输出 The number is not equal 100 !。

[root@localhost 20190105]# vi number.sh
number=200
if [ $number -eq 100 ]                 //如果number等于100则输出“The number is equal 100 !”提示
then
       echo 'The number is equal 100 !'
else                            //否则输出“The number is not equal 100 !”提示
       echo 'The number is not equal 100 !'
fi
[root@localhost 20190105]# sh number.sh
The number is not equal 100 !

Shell脚本首先判断number变量是否小于10,如果是则输出 The number < 10 !;否则,判断number变量是否大于等于10且小于20。

如果是则输出 10 =< The number < 20 !;否则,判断 number变量是否大于等于20且小于30。

如果是,则输出 20 =< The number < 30 !;否则,输出 30 <= The number !。

[root@localhost 20190105]# vi number1.sh
number=25
if [ $number -lt 10 ]              //如果number小于10
then
       echo 'The number < 10 !'
elif [ $number -ge 10 -a $number -lt 20 ] //如果number大于等于10且小于20
then
       echo '10 =< The number < 20 !'
elif [ $number -ge 20 -a $number -lt 30 ] //如果number大于等于20且小于30
then
       echo '20 =< The number < 30 !'
else                         //除上述3种情况以外的其他情况
       echo '30 <= The number !'
fi
[root@localhost 20190105]# sh number1.sh
20 =< The number < 30 !

现在你可以复制并修改测试脚本,加入else部分:

$ cp test3.sh test4.sh
$
$ nano test4.sh
$
$ cat test4.sh
#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The scropt files in the home directory of $testuser are:"
    ls /home/$testuser/*.sh
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi
echo "We are outside the if statement"
$
$ ./test4.sh
The user NoSuchUser does not exist on this system.

We are outside the if statement

这样就友好多了。跟then部分一样,else部分可以包含多条命令。fi语句说明else部分结束。 

到此这篇关于shell结构化命令if-then-else语句的文章就介绍到这了,更多相关shell if-then-else内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • linux shell中“.” 和 “./”执行的区别详解

    linux shell中“.” 和 “./”执行的区别详解

    这篇文章主要介绍了linux shell中“.” 和 “./”执行的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Linux 文件查找打包压缩及解压命令详解

    Linux 文件查找打包压缩及解压命令详解

    这篇文章主要介绍了Linux 文件查找,打包压缩及解压,文件查找和压缩解压是Linux系统中非常常见的操作,掌握了相关命令和技巧,可以提高我们的工作效率和文件管理能力,需要的朋友可以参考下
    2023-11-11
  • PowerShell实现简单的grep功能

    PowerShell实现简单的grep功能

    下面的PS脚本针对目录和文件进行了区分,借用Select-String命令,实现了内容查找,并显示查找到的文件和匹配内容所在行号。感兴趣的朋友一起看看吧
    2017-10-10
  • Shell常见知识 方便想学习linux shell的彭玉

    Shell常见知识 方便想学习linux shell的彭玉

    本文给大家介绍了一些Shell小知识,供参考学习
    2013-01-01
  • shell脚本实现定时删除文件或文件夹

    shell脚本实现定时删除文件或文件夹

    本文主要介绍了shell脚本实现定时删除文件或文件夹,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Shell脚本中调用、引用、包含另外一个脚本文件的两种方法

    Shell脚本中调用、引用、包含另外一个脚本文件的两种方法

    这篇文章主要介绍了Shell脚本中调用、引用、包含另外一个脚本文件的两种方法,本文介绍的两种方法适合在当前目录下,需要的朋友可以参考下
    2014-12-12
  • 写一个shell脚本实现视频处理

    写一个shell脚本实现视频处理

    Linux和Unix都拥有很多能够处理图像和视频文件的应用程序和工具,下面这篇文章主要给大家介绍了关于如何写一个shell脚本来实现视频处理的相关资料,需要的朋友可以参考下
    2022-07-07
  • Linux下JDK中文字体乱码的解决方法

    Linux下JDK中文字体乱码的解决方法

    下面小编就为大家带来一篇Linux下JDK中文字体乱码的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Linux换行符的使用方法详解

    Linux换行符的使用方法详解

    本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的朋友可以参考下
    2025-04-04
  • 在 Shell 提示符中显示 Git 分支名称的方法

    在 Shell 提示符中显示 Git 分支名称的方法

    这篇文章主要介绍了在 Shell 提示符中显示 Git 分支名称的方法,本文详细的讲解了解决这个需求的过程,需要的朋友可以参考下
    2015-04-04

最新评论