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

相关推荐

  • 第五周作业

    博客具体内容请移步博客园http://www.cnblogs.com/wangenzhi/p/6235456.html

    Linux干货 2016-12-30
  • 高级文件系统管理

    高级文件系统管理 本章内容  设定文件系统配额  设定和管理软RAID设备  配置逻辑卷  设定LVM快照  btrfs文件系统 配置配额系统 综述 • 在内核中执行 • 以文件系统为单位启用 • 对不同组或者用户的策略不同    &nb…

    Linux干货 2016-09-01
  • Linux下如何使用sendEmail来发送邮件

    1)什么是sendEmail sendEmail是一个轻量级,命令行的SMTP邮件客户端(注意,不要跟sendmail混淆了)。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大,从此以后你就爱上了它。 2)安装sendEmail [root@zabbix ~]# wget http://…

    系统运维 2016-07-29
  • Nginx配置进阶

    目录 ngx_http_rewrite_module模块 ngx_http_gzip_module模块 ngx_http_fastcgi_module模块 ngx_http_ssl_module模块 ngx_http_referer_module模块 ngx_http_rewrite_module模块 将用户某一次请求的URI当中的字符串是不是能够被我们给出…

    Linux干货 2016-11-05
  • N25第二周

      1,Linux上的文件管理命令都有哪些,其常用的使用方法及其相关示例演示。        文件查看类命令:cat, tac, head, tail, more, less           分屏…

    Linux干货 2016-12-12
  • php-fpm

    1.安装 mariadb 服务 修改配置文件 2.musql 安全加强 3.安装 php-fpm php-mysql php-mbstring php-mcrypt 服务 修改配置文件 4.安装httpd服务 加虚拟主机配置文件 5.测试php网页 6.安装myadmin包及测试

    2017-06-07