Linux shell if条件判断1

 

shell 逻辑控制语句:
     
     分支判断结构
       if
       case
     循环结构
        for
        while
        until
        


if语句结构

用法1

if CONDITON; then
   statement
   statement
   
fi


CONDITION条件的写法:

       COMMAND
      [ expression ]
      
expression表达式:
     
     数学表达式
     字符表达式
     文件目录表达式
     
数学表达式:
    [ number1 -eq number2 ]           等于
    [ number1 -ne number2 ]           不等于
    [ number1 -gt number2 ]           大于
    [ number1 -ge number2 ]           大于等于
    [ number1 -lt number2 ]           小于
    [ number1 -le number2 ]           小于等于
    

编写脚本,有用户输入用户名,判断用户不存在则创建

 

[root@wei shell]# vim if.sh
#!/bin/bash
#
read -p "请输入用户名: " name

id $name &> /dev/null
if [ $? -ne 0 ];then
read -p "输入密码:" passwd
useradd $name
echo "$passwd" | passwd --stdin $name &> /dev/null
echo "用户$name创建完成,初始密码为:$passwd"
fi


检测语法执行状况

[root@wei shell]# bash -x if.sh 
+ read -p '请输入用户名: ' name
请输入用户名: wei
+ id wei
+ '[' 1 -ne 0 ']'
+ read -p 输入密码: passwd
输入密码:123456
+ useradd wei
+ echo 123456
+ passwd --stdin wei
+ echo 用户wei创建完成,初始密码为:123456
用户wei创建完成,初始密码为:123456

检测语法错误

[root@wei shell]# bash -n if.sh 

条件语言脚本

用法2: 单分支if

if CONDITON; then
   statement
   statement

else
   statement
   statement
fi


编写脚本,由用户输入用户名,判断用户不存在则创建,并设置用户第一次登陆系统时需要修改密码。否则提示用户已存在

#!/bin/bash
#
read -p "请输入用户名: " name

if id $name &> /dev/null ;then
   echo " 用户$name已经存在"
else
   useradd $name
   read -p "输入密码:" passwd
   echo "$passwd" | passwd --stdin $name &> /dev/null
   passwd -e $name &> /dev/null   
   echo "用户$name创建完成,初始密码为:$passwd"
fi

由用户输入一个用户名,判断用户的UID和GID

判断的方式

[root@wei shell]# grep "hei" /etc/passwd
hei:x:1000:1000::/home/hei:/bin/bash
[root@wei shell]# grep "hei" /etc/passwd | awk -F: '{print $3,$4}'
1000 1000
[root@wei shell]# id -u hei
1000
[root@wei shell]# id -g hei
1000

脚本语法

#!/bin/bash
#
read -p "输入用户名:" name
user_id=$(id -u $name)
group_id=$(id -g $name)

if [ $user_id -eq $group_id  ];then
   echo "Good"
else
   echo "Bad"
fi


执行结果

[root@wei shell]# bash id.sh 
输入用户名:hei
Good
[root@wei shell]# id hei
uid=1000(hei) gid=1000(hei) 组=1000(hei)
[root@wei shell]# usermod -u 1200 hei
[root@wei shell]# id hei
uid=1200(hei) gid=1000(hei) 组=1000(hei)
[root@wei shell]# bash id.sh 
输入用户名:hei
Bad

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫乘风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值