OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Shell Functions

Our script currently performs the following steps to generate the HTML document:

1. Open page.

2. Open page header.

3. Set page title.

4. Close page header.

5. Open page body.

6. Output page heading.

7. Output timestamp.

8. Close page body.

9. Close page.

For our next stage of development, we will add some tasks between steps 7 and 8. These will include:

● System uptime and load. This is the amount of time since the last shutdown or re- boot and the average number of tasks currently running on the processor over sev- eral time intervals.

● Disk space. The overall use of space on the system’s storage devices.

● Home space. The amount of storage space being used by each user.


If we had a command for each of these tasks, we could add them to our script simply through command substitution:



#!/bin/bash


# Program to output a system information page


TITLE="System Information Report For $HOSTNAME" CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


cat << _EOF_

<HTML>

<HEAD>

#!/bin/bash


# Program to output a system information page


TITLE="System Information Report For $HOSTNAME" CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


cat << _EOF_

<HTML>

<HEAD>


<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

$(report_uptime)

$(report_disk_space)

$(report_home_space)

</BODY>

</HTML>

_EOF_

<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

$(report_uptime)

$(report_disk_space)

$(report_home_space)

</BODY>

</HTML>

_EOF_


We could create these additional commands two ways. We could write three separate scripts and place them in a directory listed in our PATH, or we could embed the scripts within our program as shell functions. As we have mentioned before, shell functions are “mini-scripts” that are located inside other scripts and can act as autonomous programs. Shell functions have two syntactic forms. First, the more formal form:

function name {

commands

return

}

and the simpler (and generally preferred) form:

name () {

commands

return

}

image

#!/bin/bash

#!/bin/bash

# Shell function demo


function step2 { echo "Step 2" return

}

# Main program starts here echo "Step 1"

# Shell function demo


function step2 { echo "Step 2" return

}

# Main program starts here echo "Step 1"

where name is the name of the function and commands is a series of commands contained within the function. Both forms are equivalent and may be used interchangeably. Below we see a script that demonstrates the use of a shell function:


1

2

3

4

5

6

7

8

9

10

11

12

1

2

3

4

5

6

7

8

9

10

11

12

Shell Functions


13 step2

14 echo "Step 3"

13 step2

14 echo "Step 3"


As the shell reads the script, it passes over lines 1 through 11, as those lines consist of comments and the function definition. Execution begins at line 12, with an echo com- mand. Line 13 calls the shell function step2 and the shell executes the function just as it would any other command. Program control then moves to line 6, and the second echo command is executed. Line 7 is executed next. Its return command terminates the function and returns control to the program at the line following the function call (line 14), and the final echo command is executed. Note that in order for function calls to be recognized as shell functions and not interpreted as the names of external programs, shell function definitions must appear in the script before they are called.

We’ll add minimal shell function definitions to our script:



#!/bin/bash


# Program to output a system information page


TITLE="System Information Report For $HOSTNAME" CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


report_uptime () { return

}


report_disk_space () { return

}


report_home_space () { return

}


cat << _EOF_

<HTML>

<HEAD>

<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

$(report_uptime)

$(report_disk_space)

$(report_home_space)

#!/bin/bash


# Program to output a system information page


TITLE="System Information Report For $HOSTNAME" CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


report_uptime () { return

}


report_disk_space () { return

}


report_home_space () { return

}


cat << _EOF_

<HTML>

<HEAD>

<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

$(report_uptime)

$(report_disk_space)

$(report_home_space)


</BODY>

</HTML>

_EOF_

</BODY>

</HTML>

_EOF_


Shell function names follow the same rules as variables. A function must contain at least one command. The return command (which is optional) satisfies the requirement.


Top OS Cloud Computing at OnWorks: