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

相关推荐

  • SHELL编程之数组运用及YUM软件包管理

    SHELL编程中,当要引用到多个值的时候,一个一个地进行变量赋值会让我们的脚本变得繁琐,不利于代码的优化,所以,就需要通过数组进行定义,优化代码,减少不必要的定义和命令操作。 SHELL中的数组:存储多个元素的连续内存空间 数组名:整个属组只有一个名字 数组索引: 编号从0开始   数组名[索引]   ${array_name[index…

    Linux干货 2016-08-24
  • 逻辑卷实例

    1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录 2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录 3、扩展testlv至7G,要…

    Linux干货 2016-09-01
  • Linux find命令使用详则

    find命令 find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录欲文件。并且将查找到的子目录和文件全部进行显示。 语法 find(选项)(参数) 选项 -amin<分钟>:查找在指定时间曾被存取过的文件或者目录,单位以分钟计算; -ane…

    2017-08-03
  • 正则表达式之一grep

    grep :文本过滤( 模式:pattern) 工具 包括:grep, egrep, fgrep (不 支持正则表达式 搜索) 用法格式: grep [OPTIONS] PATTERN [FILE…]            &n…

    2017-02-27
  • Ansible Conditionals & Loops

     一、条件语句     条件判断语句,就是根据某些变量的值来控制Ansible的执行流程。控制某些主机执行某些操作与不执行某些操作。根据某些操作结果,判断是否执行其它操作等等。     Ansible的条件判断语句只有 when 语句,结合变量使用才能显示出它的价值。when的用…

    Linux干货 2015-11-21
  • shell脚本基础

    1、编程基础     程序:指令+数据   程序编程风格:                         过程式:以指令为中心,数据服务于指令         &nbs…

    2017-04-20