- 设计如下⼀个菜单驱动程序,保存为: menu.sh
菜单程序将根据⽤户输⼊的选择项给出相应信息,直到输⼊Q|q才退出程序(循环),否则⼀
直提示操作信息
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today’s date and present time(如:2017-04-26 05:45:12)
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
要求对⽤户的输⼊忽略⼤⼩写,对于⽆效选项的输⼊给出相应提示。
要求使⽤case语句实现以上功能,输⼊相应的字⺟后应该执⾏相应的命令完成每项功能,如输⼊P或
p,就执⾏pwd命令。
//编写menu.sh程序
[root@localhost ~]# vim menu.sh
#!/bin/bash //.sh文件的开头
func(){
echo -e
echo "Use one of the following options:"
echo "P:To display current directory"
echo "S:To display the name of running file"
echo "D:To display today's date and present time"
echo "L:To see the list of files in your present working directory"
echo "W:To see who is logged in"
echo "I:To see the ip address of this local machine"
echo "Q:To quit this program"
echo "Enter your option and hit:"
}
while true
do
func
read input
case ${input} in
p|P)
pwd
;;
s|S)
echo $0
;;
d|D)
date "+%Y-%m-%d %H:%M:%S"
;;
l|L)
ls
;;
w|W)
who
;;
i|I)
ifconfig
;;
q|Q)
exit
;;
*)
echo "usage error!"
;;
esac
done
//测试运行结果
[root@localhost ~]# /bin/bash menu.sh
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
p
/root
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
s
menu.sh
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
D
2022-04-22 21:50:59
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
l
menu.sh
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
W
root tty1 2022-04-09 12:17
root pts/0 2022-04-22 21:47 (192.168.137.1)
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
I
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::d642:4349:cf3:cf3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1d:e4:c7 txqueuelen 1000 (Ethernet)
RX packets 12991 bytes 1018137 (994.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6131 bytes 829409 (809.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 19 base 0x2000
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 20 bytes 1760 (1.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 1760 (1.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
%
usage error!
Use one of the following options:
P:To display current directory
S:To display the name of running file
D:To display today's date and present time
L:To see the list of files in your present working directory
W:To see who is logged in
I:To see the ip address of this local machine
Q:To quit this program
Enter your option and hit:
q
[root@localhost ~]#
- 编写⼀段bash shell程序,保存为:score.sh
根据键盘输⼊的学⽣成绩,显示相应的成绩登等级,
其中
60分以下为"Failed!“,
60~69分为"Passed!”,
70~79分为"Medium!“,
80~89分为"Good!”,
90~100为"Excellent!"。
如果输⼊超过100的分数,则显示错误分数提示。
如果输⼊负数,则退出程序,否则⼀直提示⽤户输⼊成绩
[root@localhost ~]# vim score.sh
#!/bin/bash
while true
do
read score
if [ ""$score"" -lt 0 ];
then
exit
elif [ ""$score"" -lt 60 ];
then
echo "Failed!"
elif [ ""$score"" -lt 70 ];
then
echo "Passwd!"
elif [ "$score" -lt 80 ];
then
echo "Medium!"
elif [ "$score" -lt 90 ];
then
echo "Good!"
elif [ "$score" -le 100 ];
then
echo "Excellent!"
else
echo "input error!"
fi
done
[root@localhost ~]# /bin/bash score.sh
66
Passwd!
77
Medium!
88
Good!
99
Excellent!
100
Excellent!
0
Failed!
101
input error!
-1
[root@localhost ~]#
- 编写⼀段bash shell程序,保存为file.sh
判断⽤户输⼊是否为有效⽬录路径。如果不是,提示该路径不是⽬录路径。如果是
,则依次输出该⽬录下的所有内容的 “⽂件路径_⽂件类型符号”形式。
直到⽤户输⼊q退出,否则⼀直提示⽤户输⼊。
例如: _
input a directory:
/etc
/etc_d
/etc/passwd_-
[root@localhost ~]# vim file.sh
#!/bin/bash
while true
do
echo "input a directory:"
read dir
if [ "$dir" = "q" ];then
exit
elif [ -e "$dir" -a -d "$dir" ];then
for file in `ls $dir`
do
if [ ! -e "$dir$file" ];then
dd="$dir/$file"
else
dd="$dir$file"
fi
type=`ls -ld $dd | cut -c1`
echo "${dd}_${type}"
done
else
echo "$dir is not a directory"
fi
done
[root@localhost ~]# /bin/bash file.sh
input a directory:
/root
/root/file.sh_-
/root/menu.sh_-
/root/score.sh_-
input a directory:
q
[root@localhost ~]#
- 编写⼀个名为ftp.sh的shell程序,完成vsftpd的⾃动安装、配置和启动。
要求:1.采⽤stand-alone模式启动 2.禁⽌匿名启动
[root@localhost ~]# vim ftp.sh
#!/bin/bash
a=`df -Th |grep "/dev/sr0" |awk '{print $2}'`
if [ $a = iso9660 ];then
echo "You do not need to perform the operation again!"
else
echo "loading..."
mount /dev/cdrom /mnt &>/dev/null
echo "Under success!"
fi
rpm -ivh /mnt/Packages/vsftpd-3.0.2-25.el7.x86_64.rpm &> /dev/null
if [ $? = 0 ]
then
echo "Download successfully!"
else
echo "You do not need to repeat the download!"
fi
BB=/etc/vsftpd/vsftpd.conf
echo "anonymous_enable=NO" >>$BB
echo "Disabling anonymous startup succeeded!"
b=`systemctl status vsftpd |grep "Active:" |awk '{print $2}'` &>/dev/null
if [ $b = active ];then
echo "You do not need to perform the operation again!"
else
systemctl start vsftpd
echo "Open start vsftpd success!"
fi
systemctl enable vsftpd
if [ $? = 0 ];then
echo "Open enable vsftpd success!"
else
echo "Open enable vsftpd failure!"
fi
[root@localhost ~]# /bin/bash ftp.sh
ftp.sh: line 3: [: =: unary operator expected
loading...
Under success!
You do not need to repeat the download!
Disabling anonymous startup succeeded!
You do not need to perform the operation again!
Open enable vsftpd success!
[root@localhost ~]#
使用脚本安装vsftpd 参考1
使用脚本安装vsftpd 参考2
- 把menu.sh、score.sh、file.sh和ftp.sh打包成rar/tar/zip格式的压缩包,并重命名为 学号.rar | 学号.zip | 学号.tar 然后上传
yum install zip
zip 111.zip file.sh menu.sh score.sh ftp.sh
tar -cvf 111.tar file.sh menu.sh score.sh ftp.sh
rar -a 111.tar file.sh menu.sh score.sh ftp.sh