if、case 语法

1. 条件选择 if 语句

        选择执行:

             单分支

                if 判断条件: then

                  条件为真的分支代码

                fi

                   

             双分支

                if 判断条件; then

                   条件为真的分支代码

                else

                   条件为假的分支代码

                fi

                   

             多分支

                if CONDITION1; then

                    if-true

                elif CONDITION2; then

                    if-ture

                elif CONDITION3; then

                    if-ture

                    …

                else

                    all-false

                fi

   

          逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

        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

2.条件判断: case 语句

            case 变量引用 in

                PAT1)

                        分支1

                        ;;

                PAT2)

                        分支2

                        ;;

                        …

                 *)

                        默认分支

                        ;;

            esac

3.练习:

 

1、写一个脚本/root/bin/createuser.sh,实现如下功能:

使用一个用户名做为参数,如果指定参数的用户存在,就显

示其存在,否则添加之;显示添加的用户的id号等信息

read "input your username :" input_user

id $input_user

if [ $? -eq 0 ] ;then

echo" user exist"

else

useradd $input_user

chk_id=`getent passwd $input_user | cut -d: -f 3 `

echo $chk_id

fi

 

? 2、写一个脚本/root/bin/yesorno.sh,提示用户输入yes或

no,并判断用户输入的是yes还是no,或是其它信息

#!/bin/bash

#

read -p "please input yes or no:" input_info

[ -z "$input_info" ] && (echo "error";exit) || uperr_input_info=`echo "$input_info" | tr [a-z] [A-Z]`

case $uperr_input_info in 

Y|YES|NO|N)

echo "right"

;;

*)

echo "other info "

;;

esac

3、写一个脚本/root/bin/filetype.sh,判断用户输入文件路

径,显示其文件类型(普通,目录,链接,其它文件类型)

#!/bin/bash

#

read -p "please input the path :" path_file

if [ -z "$path_file" ];then

echo "you need to input info";exit

else

type_file=`ls -ld $path_file | cut -c1`

echo "the type of the file is : $type_file"

fi

4、写一个脚本/root/bin/checkint.sh,判断用户输入的参数

是否为正整数

#!/bin/bash

#

read "please input int:" int_put

if [[ "$int_put" =~ '^[1-9]+$' ]] ;then

echo "it is a int"

elif [ "$int_put" -le 0 ] ;then

ehco "it is fu int or zero"

elif [ "$int_put" -eq 0 ];then 

echo "it is zero"

else

echo "it is not a int"

fi

#也可以使用 expr a + 0 ,即可判断a的类型

       

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

(0)
ldt195175108ldt195175108
上一篇 2016-08-22 09:29
下一篇 2016-08-22 09:29

相关推荐

  • 马哥教育网络班21期-第6周课程练习

    第6周课程练习 请详细总结vim编辑器的使用并完成以下练习题   1、         复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; # cp /etc/rc.d/rc.s…

    Linux干货 2016-08-30
  • Linux之查找命令find简介

    查找命令find简介    Linux使用过程中难免会遇到查找功能,类似于Windows中的搜索功能,如果想要找某个文件在什么地方,什么格式的文件等等。在Linux中我们可以使用find、locate、which、whereis等命令。接下来我们就简单说一下如何使用这几个命令查找文件。 一、which which命令的作用是,在PATH变…

    Linux干货 2015-05-18
  • N22+张zhangzhang+第5周练习作业

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@zxn ~]# cat /etc/passwd | grep -E "^root\>" | cut -d: -f7 /bin/bash …

    Linux干货 2016-09-15
  • 编译安装httpd2.4-centos6

    centos6编译安装httpd2.4 默认是不支持,需自己动手进行编译 apr-1.4+ apr-util-1.4+ :需提前进行编译安装这两个软件 编辑安装httpd2.4实例 1、#安装开发包 [root@www ~]# yum groupinstall “Development Tools” “Server Pla…

    Linux干货 2017-05-17
  • Windows PHP 中 VC6 X86 和 VC9 X86 的区别及 Non Thread Safe 的意思

    PHP5.3以后 For Windows 提供了四个版本VC9 x86 Non Thread Safe、VC9 x86 Thread Safe、VC6 x86 Non Thread Safe、VC6 x86 Thread Safe 在 官网 左边栏有提示: Which version do I choose? If you are usi…

    Linux干货 2015-06-16
  • Linux基础知识之用户和组的配置文件解析

    实验环境:  Linux系统的版本为CentOS6.8_x86_64版本,以root用户远程用xshell连接,进行实验。 1.创建用户设置的配置文件:/etc/default/useradd        useradd 的配置文件如下图所示:        &nbs…

    Linux干货 2016-08-02