shell编程之判断语句(if、case)

1、条件测试

数值比较: [ 整数1 操作符 整数2 ]

​ [ 20 -gt 10 ] 大于

​ [ 1 -lt 2 ] 小于

​ [ 1 -eq 1 ] 等于

​ [ 1 -ne 10 ] 不等于

​ [ 29 -ge 19 ] 大于等于

​ [ 23 -le 43 ] 小于等于

文件测试: [ 操作符 文件名 ]

操作符:

-f filename : 当filename 存在时返回真

-b filename : 当filename 存在并且是块文件时返回真(返回0)
-d pathname : 当pathname 存在并且是一个目录时返回真
-h filename : 当filename 存在并且是符号链接文件时返回真 (或 -L filename)
-c filename : 当filename 存在并且是字符文件时返回真
-e pathname : 当由pathname 指定的文件或目录存在时返回真
-g pathname : 当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真
-k pathname : 当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真
-p filename : 当filename 存在并且是命名管道时返回真
-r pathname : 当由pathname 指定的文件或目录存在并且可读时返回真
-s filename : 当filename 存在并且文件大小大于0 时返回真
-S filename : 当filename 存在并且是socket 时返回真
-t fd : 当fd 是与终端设备相关联的文件描述符时返回真
-w pathname : 当由pathname 指定的文件或目录存在并且可写时返回真
-x pathname : 当由pathname 指定的文件或目录存在并且可执行时返回真
-O pathname : 当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)
-G pathname : 当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真

字符串比较: [ “字符串” = “字符串” ]

​ = 等于

​ != 不等于

​ -z 判断字符长度是为0

​ -n 判断字符的长度不为0

and 和 or:

​ and:&& 、-a 两个条件同时为真成立

​ or: ||、-o 两个条件只要有一个成立就为真

2、if

单分支结构:

​ if [ command/test ]

​ then command

​ fi

双分支结构:

​ if [ 条件测试 ]

​ then

​ 命令序列

​ else

​ 命令序列

​ fi

**多分支结构:**可以有更多分支

​ if [ 条件测试 ]

​ then

​ 命令序列

​ elif [ 条件测试2 ]

​ then

​ 命令序列

​ else

​ 命令序列

​ fi

**嵌套结构:**可多层嵌套

​ if [ 条件测试 ]

​ then

​ 命令序列

​ if [ 条件测试 ]

​ then

​ 命令序列

​ else

​ 命令序列

​ fi

​ else

​ 命令序列

​ fi

3、case

语法结构:

​ case 变量 in

​ 模式1)

​ 命令序列

​ ;;

​ 模式2)

​ 命令序列

​ ;;

​ *)

​ 无匹配后命令序列

​ ;;

​ esac

示例:

#!/bin/bash 
read -p "input username:" name
if id  $name&> /dev/null
        then
                echo "$name already exist"
                read -p"please input your password:" userpassword
                case $userpassword in
                "123456")
                echo "密码正确,登录成功!"
                ;;
                *)
                echo "密码错误,登录失败!"
                read -p"是否要找回密码?[y/n] " i
                        if [ "$i" = "y" ]
                                then
                                        read -p"please input your new password:" j
                                        if [ ${#j} -gt 3 ]
                                                then
                                                        echo "success to create!"
                                                else
                                                        echo "Too short,faile to create"
                                        fi
                                else 
                                		echo "thinks!"
                        fi
                ;;
                esac
        else
                echo "no user!"

        #               thencase $n in"yes")read -p"please input you username:" newusername
​                                read -p"input you password:" psd
​                                if [ ${#psd} -gt 7 ]thenuseradd $newusernameecho "$psd"| passwd -stdin &>/dev/null
​                                        elseecho "Invalid password,failed to create!"fi;;"no")echo "thinks for you use!";;
​                esac

        #               else echo "thinks for you use."

        #       fi

fi
sleep 1
echo "检测用户的id是否等于组id:"
read -p 'input username:' username
if id $username&>/dev/null
        then
                id1=$(id -u $username)
                id2=$(id -g $username)
                if [ $id1 -eq $id2 ];
                        then
                                echo 'yes'
                        else
                                echo 'no'
                fi
        else
                echo 'no user...'
fi
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值