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

相关推荐

  • N25期–第十五周作业

    1、 总结sed和awk的详细用法; 2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符; # sed ‘s@^[[:space:]]\+@@’ /boot/grub/grub.conf 3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符; # sed ‘s@^#[[:space:]]*@@…

    Linux干货 2017-05-08
  • mongodb 分片集群搭建

    集群架构 本次实验部署3台主机node1、node2、node3,2个副本集db1、db2作为分片,3台主机均有1个配置库实例 操作步骤 安装 node1上执行如下命令,完成后拷贝/mongodb目录到node2、node3 useradd mongodb   mkdir -p /mongodb/data/{…

    Linux干货 2016-11-06
  • vim练习题

    #复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符vim /tmp/profile%s@^[[:space:]]+@@g#复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号vim…

    Linux干货 2017-06-03
  • N22-第三周作业

    列出当前系统上所有已经登录的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# w |cut -d ' ' -f1 |sort -u lucy root USER 2.取出最后登录到当前系统的用户的相关信息。 [root@localhost ~]# last -1 root   &nbs…

    Linux干货 2016-08-28
  • Linux进程与作业1

    Linux进程与作业 操作系统的组成部分:硬件,kernel,lib, 系统调用–>调用kernel lib调用–> 库调用,kernel调用 运行用户代码–>用户模式–>用户空间; 运行kernel–>内核模式–>内核空间; 内核的功用:进程管理,文件系统,网络功能,内存管理,驱动程序,安全功能 C…

    Linux干货 2016-08-02
  • keepalived高可用haproxy配合varnish实现wordpress的动静分离

    haproxy和nginx都可以作为七层和四层反代服务器对外提供服务,此文通过haproxy和keealived配置varnish搭建wordpress的动静分离站点 一、实验环境 五台虚拟机: haproxy-1:搭建haproxy和keepalived服务,ip地址:192.168.11.176 haproxy-2:搭建haproxy和keepalive…

    2017-07-03