2 分钟阅读
https://linuxcommand.org/lc3_wss0070.php
In this lesson, we will develop some of our shell functions and get our script to produce some useful information.
在本课中,我们将开发一些 shell 函数,并使我们的脚本生成一些有用的信息。
The show_uptime
function will display the output of the uptime
command. The uptime command outputs several interesting facts about the system, including the length of time the system has been “up” (running) since its last re-boot, the number of users and recent system load.
show_uptime
函数将显示 uptime
命令的输出结果。uptime
命令输出了系统的一些有趣信息,包括系统自上次重新启动以来的运行时间,用户数量以及最近的系统负载。
|
|
To get the output of the uptime command into our HTML page, we will code our shell function like this, replacing our temporary stubbing code with the finished version:
为了将 uptime
命令的输出添加到我们的 HTML 页面中,我们将编写如下的 shell 函数,将临时的存根代码替换为最终版本:
|
|
As we can see, this function outputs a stream of text containing a mixture of HTML tags and command output. When the command substitution takes place in the main body of the our program, the output from our function becomes part of the here script.
正如我们所看到的,该函数输出了一个包含 HTML 标记和命令输出混合的文本流。当命令替换发生在我们程序的主体部分时,函数的输出将成为 here 脚本的一部分。
The drive_space function will use the df
command to provide a summary of the space used by all of the mounted file systems.
drive_space
函数将使用 df
命令提供所有已挂载文件系统使用空间的摘要信息。
|
|
In terms of structure, the drive_space
function is very similar to the show_uptime function:
在结构上,drive_space
函数与 show_uptime
函数非常相似:
|
|
The home_space
function will display the amount of space each user is using in his/her home directory. It will display this as a list, sorted in descending order by the amount of space used.
home_space
函数将显示每个用户在他/她的主目录中使用的空间量。它将按照使用空间的大小降序排列,以列表形式显示。
|
|
Note that in order for this function to successfully execute, the script must be run by the superuser, since the du
command requires superuser privileges to examine the contents of the /home directory.
请注意,为了使该函数成功执行,脚本必须以超级用户身份运行,因为 du
命令需要超级用户权限来检查 /home 目录的内容。
We’re not ready to finish the system_info
function yet. In the meantime, we will improve the stubbing code so it produces valid HTML:
我们还没有准备好完成 system_info
函数。与此同时,我们将改进存根代码,以生成有效的 HTML:
|
|