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

相关推荐

  • 日志分析工具Awstats实战之Apache篇-多站点日志分析

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1318052 前面两篇都在讲述如何去部署nginx下的awstats日志分析工具,现在终于轮到apache。作为老牌的网页服务器,awstats对apache…

    Linux干货 2016-08-15
  • Linux基础知识之帮助命令的使用

    帮助命令的使用:   内部命令     help COMMAND   外部命令:     (1)命令自带简要格式的使用帮助        COMMOND –help     (2)使用手册 manual   &nbsp…

    Linux干货 2016-07-07
  • Linux文件管理命令

    2018-03-11

    2018-03-11
  • 2018第六届中国网络安全大会即将于6月强势登陆!

    2018年6月13日,由赛可达实验室、国家计算机病毒应急处理中心、国家网络与信息系统安全产品质量监督检验中心、首都创新大联盟共同举办的第六届中国网络安全大会(NSC 2018)将在北京国家会议中心盛大召开。

    2018-03-15
  • N25_第十三周作业

    1、建立samba共享,共享目录为/data,要求:(描述完整的过程)  1)共享名为shared,工作组为magedu;  2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名;  3)添加samb…

    Linux干货 2017-03-06
  • linux文件管理及bash的基本特性

    一、常用的文件和目录管理命令1、pwd命令:用来显示当前的工作目录语法格式:直接输入pwd回车显示当前的工作目录示例:用pwd命令显示当前的工作目录[root@suyiwen ~]# pwd/root2、mkdir命令:用来创建目录文件语法格式:mkdir [OPTION]… DIRECTORY…常用option:-m,用来指定目录的…

    Linux干货 2018-03-11