![]() |
|
Some Real WorkIn this lesson, we will develop some of our shell functions and get our script to produce some useful information. show_uptimeThe 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. [me@linuxbox me]$ 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: show_uptime() { echo "<h2>System uptime</h2>" echo "<pre>" uptime echo "</pre>" } As you 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. drive_spaceThe drive_space function will use the df command to provide a summary of the space used by all of the mounted file systems. [me@linuxbox me]$ df Filesystem 1k-blocks Used Available Use% Mounted on In terms of structure, the drive_space function is very similar to the show_uptime function: drive_space() { echo "<h2>Filesystem space</h2>" echo "<pre>" df echo "</pre>" } home_spaceThe 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() { echo "<h2>Home directory space by user</h2>" echo "<pre>" echo "Bytes Directory" du -s /home/* | sort -nr echo "</pre>" } 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. system_infoWe'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() { echo "<h2>System release info</h2>" echo "<p>Function not yet implemented</p>" } |
|
© 2000-2018, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved. Linux® is a registered trademark of Linus Torvalds. |