在 if 语句里面,你可以使用另外一个 if 语句。只要你能逻辑管理
你就可以使用多层嵌套。 以下是一个测试闰年的例子:
#!/bin/bash
# This script will test if we're in a leap year or not.
year=`date +%Y`
if [ $[$year % 400] -eq "0" ]; then  
   echo "This is a leap year.  February has 29 days."
elif [ $[$year % 4] -eq 0 ]; then        
       if [ $[$year % 100] -ne 0 ]; then          
          echo "This is a leap year, February has 29 days."        
       else          
          echo "This is not a leap year.  February has 28 days."        
       fi
else   echo "This is not a leap year.  February has 28 days."
fi
# echo `date +%Y` 2009
# ./leapyear.sh This is not a leap year.  February has 28 days.
--------------------------------------------------------------------
以上的脚本可以用布尔操作符 “AND(&&)” 和 “OR(||)” 来缩短
#!/bin/bash
# This script will test if we're in a leap year or not.
year=`date +%Y`
if (( ("$year" % 400) == "0" )) || ((("$year" % 4 == "0") && ("$year" % 100 != "0") )); then  
      echo "This is a leap year." else   echo "This is not a leap year."
fi
# ./leapyear2.sh This is not a leap year.
--------------------------------------------------------------------------
同样可以使用内建命令read来捕捉用户的输入,来测试是否为闰年:
#!/bin/bash
# This script will test if you have given a leap year or not.
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )); then  
   echo "$year is a leap year."
else  
   echo "This is not a leap year."
fi
# ./leap2.sh
Type the year that you want to check (4 digits), followed by [ENTER]: 2009 This is not a leap year.

if嵌套语句 shell脚本实例 测试是否闰年的更多相关文章

  1. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

  2. 常见的if语句shell脚本

    常见的if语句shell脚本 author :headsen  chen  2017-10-17  15:00:07 1,cat if.sh 2, cat  if2.sh

  3. 二、shell 脚本条件测试

    目录 一.条件测试 test 格式 文件测试 文件测试常见选项 整数值比较 字符串比较 浮点数的运算 逻辑测试 二.if语句 1单分支 2双分支结构 3多分支结构 三元运算符 三.case 一.条件测 ...

  4. 如何进行shell脚本正确性测试

    在实际工作中,需要对shell脚本进行正确性测试. 如何用最快最有效的方式进行测试? 很多开发的习惯是,二话不说,写完/拿到,就跑一把,看看输入,输出,想要的操作是否完成,也就过了. 其实这是十分不严 ...

  5. shell脚本实例一

    一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim  test.sh     ##shell脚本一般都 ...

  6. shell脚本实例,通向shell脚本大师的必经之路

    概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...

  7. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  8. shell脚本实例

    备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...

  9. shell脚本实例一,移动文件夹中大于2000B的文件到另一个文件夹

    shell脚本能帮我们简化linux下的一些工作,现在有个需求,把TMPA文件夹下大于2000B的文件都移动到TMPB下 #! /bin/bash function movefiles() { ` d ...

随机推荐

  1. HDU 1847 Good Luck in CET-4 Everybody! (博弈)

    题意:不用说了吧,都是中文的. 析:虽说这是一个博弈的题,但是也很简单的,在说这个题目前我们先说一下巴什博弈定理. 巴什博弈定理:一堆物品有n个,有两个人(两个人足够聪明)轮流取,规定每次至少取一个, ...

  2. 一)如何开始 ehcache ?

    官网地址 http://www.ehcache.org/ 从哪开始 第一步优先下载 http://www.ehcache.org/downloads/ 下载 Ehcache 2.10.0 .tar.g ...

  3. QGIS Server Quickstart

    http://live.osgeo.org/en/quickstart/qgis_mapserver_quickstart.html

  4. 18、docker的持久化存储和数据共享

    18.1 Data Volume Docker持久化数据方案 基于本地文件系统的Volume   可以在执行docker create或者docker run的时候,通过-v参数将主机的目录作为容器的 ...

  5. [Ubuntu]管理开机启动项的软件

    sudo apt-get install sysv-rc-conf

  6. LeetCode137:Single Number II

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  7. NPOI datatable导出类

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Generic; ...

  8. C# 读取Excel,一波华丽的操作

    C# 读取Excel,其实有很多方法.但是今天要来一波华丽的操作. 先看效果: 以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的. Exc ...

  9. 缓存 - 内存数据库Redis

    客户端 Redis Desktop Manager 官网 新版的收费,或者要分享什么的.0.8.8的旧版才免费?:https://github.com/uglide/RedisDesktopManag ...

  10. 为控件动态添加Style

    此文可解决:  重写控件时,给控件加入子控件或父控件的样式切换问题. 很灵活的可以根据不同内容显示不同样式 子控件作用在: <DataTemplate x:Key="ColmunHea ...