shell脚本练习
2021/12/1 7:09:22
本文主要是介绍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
这篇关于shell脚本练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南