OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

A More Complete Application

After a long hiatus, we are going to resume work on our sys_info_page program. Our next addition will add several command line options to the program as follows:

Output file. We will add an option to specify a name for a file to contain the pro- gram’s output. It will be specified as either -f file or --file file.

Interactive mode. This option will prompt the user for an output filename and will determine if the specified file already exists. If it does, the user will be prompted before the existing file is overwritten. This option will be specified by either -i or --interactive.

Help. Either -h or --help may be specified to cause the program to output an informative usage message.

Here is the code needed to implement the command line processing:



usage () {

echo "$PROGNAME: usage: $PROGNAME [-f file | -i]" return

}

# process command line options interactive=

usage () {

echo "$PROGNAME: usage: $PROGNAME [-f file | -i]" return

}

# process command line options interactive=


filename=


while [[ -n $1 ]]; do case $1 in

-f | --file)

shift

filename=$1

;;

filename=


while [[ -n $1 ]]; do case $1 in

-f | --file)


-i | --interactive) interactive=1

;;

-h | --help) usage exit

;;

*) usage >&2

exit 1

;;

-i | --interactive) interactive=1

;;

-h | --help) usage exit

;;

*) usage >&2

exit 1

;;


esac

shift done

esac

shift done


image

First, we add a shell function called usage to display a message when the help option is invoked or an unknown option is attempted.

Next, we begin the processing loop. This loop continues while the positional parameter

$1 is not empty. At the end of the loop, we have a shift command to advance the posi- tional parameters to ensure that the loop will eventually terminate.

Within the loop, we have a case statement that examines the current positional parame- ter to see if it matches any of the supported choices. If a supported parameter is found, it is acted upon. If an unknown choice is founc, the usage message is displayed and the script terminates with an error.

The -f parameter is handled in an interesting way. When detected, it causes an additional shift to occur, which advances the positional parameter $1 to the filename argument supplied to the -f option.

We next add the code to implement the interactive mode:



# interactive mode


if [[ -n $interactive ]]; then while true; do

read -p "Enter name of output file: " filename if [[ -e $filename ]]; then

read -p "'$filename' exists. Overwrite? [y/n/q] > " case $REPLY in

Y|y) break

# interactive mode


if [[ -n $interactive ]]; then while true; do

read -p "Enter name of output file: " filename if [[ -e $filename ]]; then

read -p "'$filename' exists. Overwrite? [y/n/q] > " case $REPLY in

Y|y) break


;;

Q|q) echo "Program terminated." exit

;;

*) continue

;;

esac

elif [[ -z $filename ]]; then continue

else

break

fi

done

fi

;;

Q|q) echo "Program terminated." exit

;;

*) continue

;;

esac

elif [[ -z $filename ]]; then continue

else

break

fi

done

fi


If the interactive variable is not empty, an endless loop is started, which contains the filename prompt and subsequent existing file-handling code. If the desired output file already exists, the user is prompted to overwrite, choose another filename, or quit the program. If the user chooses to overwrite an existing file, a break is executed to termi- nate the loop. Notice how the case statement only detects if the user chooses to over- write or quit. Any other choice causes the loop to continue and prompts the user again.

In order to implement the output filename feature, we must first convert the existing page-writing code into a shell function, for reasons that will become clear in a moment:



write_html_page () { 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_ return

}


# output html page


if [[ -n $filename ]]; then

write_html_page () { 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_ return

}


# output html page


if [[ -n $filename ]]; then


if touch $filename && [[ -f $filename ]]; then write_html_page > $filename

else

echo "$PROGNAME: Cannot write file '$filename'" >&2 exit 1

fi else

write_html_page

fi

if touch $filename && [[ -f $filename ]]; then write_html_page > $filename

else

echo "$PROGNAME: Cannot write file '$filename'" >&2 exit 1

fi else

write_html_page

fi


The code that handles the logic of the -f option appears at the end of the listing shown above. In it, we test for the existence of a filename and, if one is found, a test is per - formed to see if the file is indeed writable. To do this, a touch is performed, followed by a test to determine if the resulting file is a regular file. These two tests take care of sit - uations where an invalid pathname is input (touch will fail), and, if the file already ex- ists, that it’s a regular file.

As we can see, the write_html_page function is called to perform the actual gener- ation of the page. Its output is either directed to standard output (if the variable file- name is empty) or redirected to the specified file.


Top OS Cloud Computing at OnWorks: