shell脚本练习

计算机网络老师 16:32:26
任务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

计算机网络老师 16:32:43
任务2:使用if语句编写shell脚本
1、使用if判断语句添加用户
NAME=user1
if  id KaTeX parse error: Expected 'EOF', got '&' at position 6: NAME &̲> /dev/null ; t…NAME is exist."
        else
           useradd N A M E               e c h o " NAME             echo " NAME       echo"NAME" | passwd --stdin KaTeX parse error: Expected 'EOF', got '&' at position 6: NAME &̲> /dev/null    …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 KaTeX parse error: Expected 'EOF', got '#' at position 75: …81-100分显示nice。 #̲!/bin/bash read…score” -ge 0 -a “ s c o r e " − l e 60 ] ; t h e n e c h o " f a i l " e l i f [ “ score" -le 60 ];then echo "fail" elif [“ score"−le60];thenecho"fail"elif[“score” -gt 60 -a " s c o r e " − l e 80 ] ; t h e n e c h o " G O O D ! " e l i f [ “ score" -le 80 ];then echo "GOOD! " elif [“ score"−le80];thenecho"GOOD!"elif[“score”-gt 8e -a “$score” -le 100 ];then
echo"Nice”
else
echo “wrong score”
fi

计算机网络老师 16:32:51
#完成任务1、2后请接龙:
1、XXX,已完成任务1、2

计算机网络老师 16:46:22
if [[ “KaTeX parse error: Expected 'EOF', got '&' at position 13: x" -ge "90" &̲& "x” -le “100” ]];then
echo “Congratulation,Your grade is A.”
elif [[ “KaTeX parse error: Expected 'EOF', got '&' at position 13: x" -ge "80" &̲& "x” -le “89” ]];then
echo “Good,Your grade is B.”
elif [[ “KaTeX parse error: Expected 'EOF', got '&' at position 13: x" -ge "70" &̲& "x” -le “79” ]];then
echo “Ok.Your grade is C.”
elif [[ “KaTeX parse error: Expected 'EOF', got '&' at position 13: x" -ge "60" &̲& "x” -le “69” ]];then
echo “Yeah,Your grade is D.”
elif [[ “$x” -lt “60” ]];then
echo “Right,Your grade is F.”
else
echo “Unknow argument…”
fi

上一篇:使用RTL SDR 和VirtualRadar看飞机的问题解决记录


下一篇:痞子衡嵌入式:关于i.MXRT中FlexSPI外设lookupTable里配置Normal read的一个小误区