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

相关推荐

  • N26-第四周

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -a /etc/skel   /home/tuser1 [root@localhost ~]# chmod go-r  -R /home/tuser…

    Linux干货 2017-02-22
  • LVS中nat和dr的原理与用法简述

      LVS中nat和dr的原理与用法简述 lvs-nat:   多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发; (1)RIP和DIP必须在同一个IP网络,且应该使用私网地址;RS的网关要指向DIP; (2)请求报文和响应报文都必须经由Director转发;Director…

    Linux干货 2016-10-30
  • 功能强大的Linux文本编辑器之Vim的使用

    VIM编辑器   Vim章节的内容:    使用vi和vim的三种主要模式    移动光标,进入插入模式    改变、删除、复制文本    撤销改变    搜索文档    vim寄存器    可视化和多窗口 &…

    Linux干货 2016-08-12
  • 关于加密那点事

    作者【Jev Tse】 环境:sentos6.8     【本文预览】      一、关于加密      二、对称加密      三、非对称加密      四、单向散列 …

    Linux干货 2016-12-01
  • sed命令的用法和vim编辑器的使用

    一、文本处理工具sed sed [option]….'script] [input-file]… 简介: sed是一种流编辑器,他一次处理一行的内容,处理时把当前处理的行存储在临时缓冲区中,这个缓冲区被称为模式空间,接着用sed命令处理,处理完成后把模式空间的内容送往屏幕,接着处理下一行,知道文件末尾,而文件的内容…

    Linux干货 2016-08-15
  • FTP部署及简单应用

    文件服务: ftp:应用层,C/S,文件共享;file transfer protocol; nfs,cifs:文件系统接口,网络文件系统;     nfs:network file system     cifs:common …

    Linux干货 2016-10-19