Shell脚本的编写及测试

                                                  Shell脚本的编写及测试

1.1问题

本例要求两个简单的Shell脚本程序,任务目标如下:

编写一个面世问候/root/helloworld.sh脚本,执行后显示出一段话“Hello World!!”

编写一个能输出系统信息的/root/sysinfo脚本,执行后依次输出当前红帽系统的版本信息、当前使用的内核版本、当前系统的主机名

1.2方案

规范Shell脚本的一般组成:

#!环境声明(Sha-Bang

#注释文本

可执行代码

1.3步骤

实现此案例需要按照如下步骤进行。

步骤一:编写helloworld.sh问候脚本

1)编写脚本代码

[root@server0~]#vim /root/helloworld.sh

#!/bin/bash

echo"Hello World!!"

2)添加x执行权限

[root@server0~]#chmod +x /root/helloworld.sh

3)运行脚本测试

[root@server0~]#/root/helloworld.sh

Hello World!!

步骤二:编写sysinfo系统信息报告脚本

1)编写脚本代码

[root@server0~]#vim /root/sysinfo

#!/bin/bash

cat /etc/redhat-release

uname-r

hostname

2)添加x执行权限

[root@server0~]#chmod +x /root/sysinfo

3)运行脚本测试

[root@server0~]#/root/sysinfo

Red Hat Enterprise Linux Server release 7.0(Maipo)

3.10.0-123.el7.x86_64zhsan

lisi

2)实现批量添加用户:

[root@server0~]#/root/batchusers/root/userlist

[root@server0~]#id duanwu

uid=1006(duanwu)gid=1006(duanwu)groups=1006(duanwu)

3)测试其他异常处理:

[root@server0~]#/root/batchusers//未提供列表文件

Usage:/root/batchusers<userfile>

[root@server0~]#echo$?

1

[root@server0~]#/root/batchusers/root/userlist.txt//提供的列表文件找不到

Input file not found

[root@server0~]#echo$?

2

server0.example.com

原文地址:https://www.cnblogs.com/qingbai/p/11936723.html