OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Keep Scripts Running

While developing our program, it is useful to keep the program in a runnable state. By doing this, and testing frequently, we can detect errors early in the development process. This will make debugging problems much easier. For example, if we run the program, make a small change, then run the program again and find a problem, it’s very likely that the most recent change is the source of the problem. By adding the empty functions, called stubs in programmer-speak, we can verify the logical flow of our program at an early stage. When constructing a stub, it’s a good idea to include something that provides feedback to the programmer, which shows the logical flow is being carried out. If we look at the output of our script now:



[me@linuxbox ~]$ sys_info_page

<HTML>

<HEAD>

<TITLE>System Information Report For twin2</TITLE>

[me@linuxbox ~]$ sys_info_page

<HTML>

<HEAD>

<TITLE>System Information Report For twin2</TITLE>


</HEAD>

<BODY>

<H1>System Information Report For linuxbox</H1>

<P>Generated 03/19/2009 04:02:10 PM EDT, by me</P>


</BODY>

</HTML>

</HEAD>

<BODY>

<H1>System Information Report For linuxbox</H1>

<P>Generated 03/19/2009 04:02:10 PM EDT, by me</P>


</BODY>

</HTML>


we see that there are some blank lines in our output after the timestamp, but we can’t be sure of the cause. If we change the functions to include some feedback:



report_uptime () {

echo "Function report_uptime executed."

return

}


report_disk_space () {

echo "Function report_disk_space executed."

return

}


report_home_space () {

echo "Function report_home_space executed."

return

}

report_uptime () {

echo "Function report_uptime executed."

return

}


report_disk_space () {

echo "Function report_disk_space executed."

return

}


report_home_space () {

echo "Function report_home_space executed."

return

}


and run the script again:



[me@linuxbox ~]$ sys_info_page

<HTML>

<HEAD>

<TITLE>System Information Report For linuxbox</TITLE>

</HEAD>

<BODY>

<H1>System Information Report For linuxbox</H1>

<P>Generated 03/20/2009 05:17:26 AM EDT, by me</P> Function report_uptime executed.

Function report_disk_space executed. Function report_home_space executed.

</BODY>

</HTML>

[me@linuxbox ~]$ sys_info_page

<HTML>

<HEAD>

<TITLE>System Information Report For linuxbox</TITLE>

</HEAD>

<BODY>

<H1>System Information Report For linuxbox</H1>

<P>Generated 03/20/2009 05:17:26 AM EDT, by me</P> Function report_uptime executed.

Function report_disk_space executed. Function report_home_space executed.

</BODY>

</HTML>

Keep Scripts Running


we now see that, in fact, our three functions are being executed.

With our function framework in place and working, it’s time to flesh out some of the function code. First, the report_uptime function:


report_uptime () {

cat <<- _EOF_

<H2>System Uptime</H2>

<PRE>$(uptime)</PRE>

_EOF_

return

}

report_uptime () {

cat <<- _EOF_

<H2>System Uptime</H2>

<PRE>$(uptime)</PRE>

_EOF_

return

}


It’s pretty straightforward. We use a here document to output a section header and the output of the uptime command, surrounded by <PRE> tags to preserve the formatting of the command. The report_disk_space function is similar:


report_disk_space () {

cat <<- _EOF_

<H2>Disk Space Utilization</H2>

<PRE>$(df -h)</PRE>

_EOF_

return

}

report_disk_space () {

cat <<- _EOF_

<H2>Disk Space Utilization</H2>

<PRE>$(df -h)</PRE>

_EOF_

return

}


This function uses the df -h command to determine the amount of disk space. Lastly, we’ll build the report_home_space function:


report_home_space () {

cat <<- _EOF_

<H2>Home Space Utilization</H2>

<PRE>$(du -sh /home/*)</PRE>

_EOF_

return

}

report_home_space () {

cat <<- _EOF_

<H2>Home Space Utilization</H2>

<PRE>$(du -sh /home/*)</PRE>

_EOF_

return

}


We use the du command with the -sh options to perform this task. This, however, is not a complete solution to the problem. While it will work on some systems (Ubuntu, for ex- ample), it will not work on others. The reason is that many systems set the permissions of home directories to prevent them from being world-readable, which is a reasonable secu- rity measure. On these systems, the report_home_space function, as written, will


image

only work if our script is run with superuser privileges. A better solution would be to have the script adjust its behavior according to the privileges of the user. We will take this up in the next chapter.


Shell Functions In Your .bashrc File

Shell functions make excellent replacements for aliases, and are actually the pre- ferred method of creating small commands for personal use. Aliases are very lim- ited in the kind of commands and shell features they support, whereas shell func- tions allow anything that can be scripted. For example, if we liked the report_disk_space shell function that we developed for our script, we could create a similar function named ds for our .bashrc file:

ds () {

echo “Disk Space Utilization For $HOSTNAME” df -h

}


Top OS Cloud Computing at OnWorks: