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 expr 命令的使用小结

    Linux expr 命令的使用小结

    expr是命令行计算器,一般用在数字和字符串上面,本篇文章给大家介绍Linux expr 命令的使用小结,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • linux中&和&&,|和||及分号(;)的用法

    linux中&和&&,|和||及分号(;)的用法

    本文主要介绍了linux中&和&&,|和||及分号(;)的用法,我们经常会用到,但是很多人会混淆,下面就来介绍一下具体用法,巩固的大家的记忆,感兴趣的可以了解一下
    2023-06-06
  • SHELL中pr命令的实现

    SHELL中pr命令的实现

    pr是 “print” 的缩写,主要用于将文本文件分页,并添加页眉、页码等信息,本文就来详细的介绍一下SHELL中pr命令的实现,感兴趣的可以了解一下
    2025-09-09
  • 写一个shell脚本实现视频处理

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

    Linux和Unix都拥有很多能够处理图像和视频文件的应用程序和工具,下面这篇文章主要给大家介绍了关于如何写一个shell脚本来实现视频处理的相关资料,需要的朋友可以参考下
    2022-07-07
  • Shell 管道及执行顺序分析

    Shell 管道及执行顺序分析

    Shell 管道及执行顺序分析,需要的朋友可以参考下
    2013-01-01
  • linux shell常用循环与判断语句(for,while,until,if)使用方法

    linux shell常用循环与判断语句(for,while,until,if)使用方法

    这篇文章主要介绍了linux shell循环,判断介绍,以及实例使用方法,需要的朋友可以参考下
    2019-04-04
  • linux定时器crontab的使用教程

    linux定时器crontab的使用教程

    这篇文章主要介绍了linux定时器crontab的使用教程,需要的朋友可以参考下
    2018-08-08
  • 一个简单的linux命令 tail

    一个简单的linux命令 tail

    这篇文章主要介绍了一个简单的linux命令tail,tail命令用来从指定点开始将文件写到标准输出,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • shell正则表达式、grep命令和sed命令举例详解

    shell正则表达式、grep命令和sed命令举例详解

    正则表达式就是用于描述这些规则的工具,换句话说正则表达式就是记录文本规则的代码,这篇文章主要给大家介绍了关于shell正则表达式、grep命令和sed命令的相关资料,需要的朋友可以参考下
    2024-02-02
  • shell脚本环境变量.bashrc配置实践

    shell脚本环境变量.bashrc配置实践

    本文介绍了Linux中的环境变量配置方法,包括/etc/profile和~/.bashrc,作者强调了bash脚本环境变量的重要性,并提出了三种解决方法:在脚本中加入环境变量、配置~/.bashrc和将/etc/profile加入~/.bashrc
    2026-02-02

最新评论