Linux的shell编写

-eq           //等于
-ne           //不等于
-gt            //大于
-lt            //小于
ge            //大于等于
le            //小于等于

实验中遇到的问题:
1.NAME=user1 中间不能有空格
2.[ xxx ] xxx前面和后面要有空格


任务1:使用case语句编写shell程序
1、由用户从键盘输入一个字符,并判断该字符是否为字母、数字或者其他字符, 并输出相应的提示信息。
代码:
#!/bin/bash
read -p "press some key ,then press return :" KEY
case $KEY in
[a-z]|[A-Z])
echo "It's a letter."
;;
[0-9]) 
echo "It's a digit."
;;
*)
echo "It's function keys、Spacebar or other ksys."
esac
2、判断在执行程序时所输入的参数,符合相应条件,输出相对应的信息
#!/bin/bash
case $1 in
        start | begin)
          echo "start something"
        ;;
        stop | end)
          echo "stop something"
        ;;
        *)
          echo "Ignorant"
        ;;
esac
 3、判断当前系统属于哪种操作系统,并执行相应分支的输出语句
#!/bin/bash
 SYSTEM=`uname -s` 
 case $SYSTEM in 
     Linux) 
         echo "My system is Linux" 
         echo "Do Linux stuff here..." 
     ;; 
     FreeBSD) 
         echo "My system is FreeBSD" 
         echo "Do FreeBSD stuff here..." 
     ;; 
     *) 
         echo "Unknown system : $SYSTEM" 
         echo "I don't what to do..." 
     ;; 
 esac

结果:
1和2
在这里插入图片描述
在这里插入图片描述

任务2:使用if语句编写shell脚本
1、使用if判断语句添加用户
NAME=user1
if  id $NAME &> /dev/null ; then
           echo "$NAME is exist."
        else
           useradd $NAME
           echo "$NAME" | passwd --stdin $NAME &> /dev/null
           echo "$NAME is add sucessful"
     fi
2、如果用户已存在,显示已存在,则删除此用户并删除此用户的家目录,并提示删除成功;如果不存在,提示用户不存在。
#!/bin/bash
NAME=user1
      if ! id $NAME &> /dev/null ; then
           echo "The $NAME is not exist."
        else
           userdel -r $NAME &> /dev/null
           echo "The $NAME is del successful."
       fi
3、给定一个用户,如果其UID为0,就显示此为管理员;否则,就显示其为普通用户;
#!/bin/bash
read -p "please input username:" NAME
NAMEID=`id -u $NAME`
if [ $NAMEID -eq 0 ]; then
        echo "The $NAME is root."
else
        echo "The $NAME is common."
fi
4、输入分数,60分以下显示失败,60-80分显示good,81-100分显示nice。
#!/bin/bash
read -p "please input your score:" score
if [ "$score" -ge 0 -a "$score" -le 60 ];then
        echo "fail"
elif [ "$score" -gt 60 -a "$score" -le 80 ];then
        echo "GOOD! "
elif [ "$score" -gt 80 -a "$score" -le 100 ];then
        echo "Nice"
else
        echo "wrong score"
fi

结果:
1.创建用户:
这样是创建了一个用户名为空的用户:

在这里插入图片描述
这样是创建了一个用户名为zhengjunsong的用户:
在这里插入图片描述
2.删除用户
把刚才的用户名为空的用户删掉:
在这里插入图片描述
3.给定一个用户,如果其UID为0,就显示此为管理员;否则,就显示其为普通用户;
在这里插入图片描述
4.输入分数,60分以下显示失败,60-80分显示good,81-100分显示nice。
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值