shell脚本编程之if、case条件语句

程序执行三种顺序

    顺序执行

    22.png

    选择执行

    shell脚本编程之if、case条件语句

    循环执行

    shell脚本编程之if、case条件语句

    shell脚本编程之if、case条件语句

选择执行语句:

    if语句 

    格式:

    单分支:    

    if 判断条件;then

      条件分支代码

    fi

    

     双分支:

    if 判断条件;then

     条件为真时语句

    else (此处不会对给出的条件做判断)

     条件为假时语句

    fi

  

     多分支:

    if 条件1;then

     代码

    elif 条件2;then

     代码

     elid 条件2;then

     代码

    else

      代码

    fi

    例子:输入两个数,判断其大小

        [root@centos7 bin]# cat f1
        #!/bin/bash
        read -p "input a number:" m
        read -p "input a number:" n
        if [ $m -gt $n ];then 
	    echo "$m >$n"
        else [ $m = $n ]                此程序中,else后面的判断无效,如果m=n或者m<n都会走else分支,若想只要条件符合的输出需要将else换成elif
	    echo " $m = $n"
        fi
        [root@centos7 bin]# 
        [root@centos7 bin]#

if语句例子

   if ping – c1 – W2 station1 &> /dev/null; then
    echo 'Station1 is UP'
   elif grep "station1" ~/maintenance.txt &> /dev/null
   then
    echo 'Station1 is undergoing maintenance‘
   else
    echo 'Station1 is unexpectedly DOWN!'
    exit 1
   fi
 

条件判断case

  case 变量 in

   pat1)

    分支1

    ;;

   part2)

    分支2

    ;;

   part3)

    分支3

    ;;

   esac

 注意:case 支持glob的通配符

      
    循环语句综合练习:
        1、写一个脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显
        示其存在,否则添加之;显示添加的用户的id号等信息
        2、写一个脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
        3、写一个脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
        4、写一个脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
  
       答案:1
       [root@centos7 bin]# cat useradd.sh 
        #!/bin/bash
        read -p "please input the username:" username
        if id -u $username &>/dev/null ;then
	    echo "the user $username exit"
        else 
	        useradd $username
        echo "useradd success,userID is `id -u $username`"
	
        fi
        [root@centos7 bin]# 
        
        答案:2
        [root@centos7 bin]# cat caseyesorno.sh 
        #!/bin/bash
        read -p  "please input yes or no:" yn
        case  $yn in
        yes|y|0|Y|[yY][Ee][Ss])
        echo "you input is: $yn"
        ;;

        no|[Nn][Oo]|N|n)
	echo "you input is:$yn"
        ;;
        *)
	echo "you input is other"
        esac
        [root@centos7 bin]# 
        
        
        
        [root@centos7 bin]# cat  yesorno.sh 
        #!/bin/bash
        read -p  "please input yes or no:" yn
        if   [[ $yn =~ [yY][eE][sS] ||[Yy] ]] ;then
            echo "you input is: yes"
        elif [[ $yn =~ [nN][oO] || [Nn] ]];then
	    echo "you input is:no"
        else 
	    echo "you input is other"
        fi
        [root@centos7 bin]#
        

    答案:3
        [root@centos7 bin]# cat filetype.sh 
            #!/bin/bash
            read -p "please input filename:" filename
            [ !  -e $filename ] && echo "filename not exit"&&exit
            if [ -h $filename ] ;then                                    注意:软链接文件也被识别为普通文件,正确判断出软链接文件,将-h条件放到-f文件前去判断
	        echo "filetype is symolink  file"
            elif [ -d $filename ] ;then
        	echo "filetype is dirction"
            elif [ -f $filename ] ;then
	        echo "filetype is common file"
            else 
	        echo "filetype is other"
            fi
            [root@centos7 bin]#
        答案:4
        
            [root@centos7 bin]# cat checkint.sh 
            #!/bin/bash
            read -p "please input a num:" num
            if [ $num -lt 1 ] ;then
	        echo "please input a init"
            elif [[ $num =~  ^[[:digit:]]+$ ]];then 
        	echo "you input is a int"
            else 
	        echo "input error" 
            fi
            [root@centos7 bin]#

    

    

        

原创文章,作者:wangnannan,如若转载,请注明出处:http://www.178linux.com/35866

(0)
wangnannanwangnannan
上一篇 2016-08-18 10:10
下一篇 2016-08-18 10:10

相关推荐

  • 用vsftpd来搭建网络yum源

    1、分别在CentOS6和CentOS7分别搭建网络yum源 a、CentOS 7中 首先安装并启用vsftpd服务 [root@node1 ~]# mount /dev/cdrom /mnt/yum/ [root@node1 ~]# vim /etc/yum.repos.d/local.repo [local] name=local baseurl=fil…

    2015-10-03
  • Shell脚本编程初步

        shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。shell脚本(shell script),是一种为shell编写的脚本程序。业界所说的shell通常都是指shell脚本,但shell和shell script是两个不同的概念。shell编程跟java、php编程…

    Linux干货 2016-08-24
  • 高级文件系统管理的相关理解

    1.RAID是什么     RAID:Redundant Arrays of Inexpensive Disks,是指由多个磁盘合成一个阵列,来提供更好的性能,冗余或者两者都提供。在生产生活中,通常一个硬盘往往不能满足我们的存储需要,这是就需要通过RAID磁盘阵列来对磁盘进行“扩容”,来满足我们的日常需要,相比较于单一…

    Linux干货 2016-08-30
  • Python语法基础之if while for常见的简单算法

    格式要符合语法要求

    2017-09-14
  • CentOS 系统启动流程

    CentOS 系统启动流程 一、Linux系统的组成部分:内核+根文件系统 1.内核: 进程管理:进程之间的通信为:IPC(Inter Process Communication)机制,有消息队列、semerphor、shm、socket(跨主机之间的通信) 内存管理; 网络管理; 文件系统; 驱动程序; 安全功能; 2.运行中的系统环境可分为两层:内核空间…

    Linux干货 2016-09-13
  • Linux 进程及作业管理

    Linux 进程及作业管理 概述:      我们在实际的运维工作中经常要对服务器做各种监控,以了解其相关状态,比如,CPU利用率,磁盘空间利用率,等等,这就需要我们了解相关的数据并学会使用各种工具来分析数据。本章就将总结Linux系统中进程管理和作业管理的相关命令、参数和工具,包括:进程的相关定义、分类、状态以及进程查看和管理…

    Linux干货 2016-09-11