OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Summing Up

With the addition of positional parameters, we can now write fairly functional scripts. For simple, repetitive tasks, positional parameters make it possible to write very useful shell functions that can be placed in a user’s .bashrc file.

Our sys_info_page program has grown in complexity and sophistication. Here is a complete listing, with the most recent changes highlighted:



#!/bin/bash


# sys_info_page: program to output a system information page


PROGNAME=$(basename $0)

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

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


report_uptime () { cat <<- _EOF_

<H2>System Uptime</H2>

<PRE>$(uptime)</PRE>

_EOF_ return

#!/bin/bash


# sys_info_page: program to output a system information page


PROGNAME=$(basename $0)

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

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


report_uptime () { cat <<- _EOF_

<H2>System Uptime</H2>

<PRE>$(uptime)</PRE>

_EOF_ return


}


report_disk_space () { cat <<- _EOF_

<H2>Disk Space Utilization</H2>

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

_EOF_ return

}


report_home_space () {

if [[ $(id -u) -eq 0 ]]; then cat <<- _EOF_

<H2>Home Space Utilization (All Users)</H2>

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

_EOF_

else

cat <<- _EOF_

<H2>Home Space Utilization ($USER)</H2>

<PRE>$(du -sh $HOME)</PRE>

_EOF_

fi return

}


usage () {

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

}


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

}


# process command line options

}


report_disk_space () { cat <<- _EOF_

<H2>Disk Space Utilization</H2>

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

_EOF_ return

}


report_home_space () {

if [[ $(id -u) -eq 0 ]]; then cat <<- _EOF_

<H2>Home Space Utilization (All Users)</H2>

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

_EOF_

else

cat <<- _EOF_

<H2>Home Space Utilization ($USER)</H2>

<PRE>$(du -sh $HOME)</PRE>

_EOF_

fi return

}


usage () {

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

}


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

}


# process command line options

image

Summing Up


interactive=

filename=


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

-f | --file)

shift

filename=$1

;;

interactive=

filename=


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

-f | --file)


-i | --interactive) interactive=1

;;

-h | --help) usage exit

;;

*) usage >&2

exit 1

;;

esac shift

done


# 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


# output html page


if [[ -n $filename ]]; then

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

else

-i | --interactive) interactive=1

;;

-h | --help) usage exit

;;

*) usage >&2

exit 1

;;

esac shift

done


# 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


# 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

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

fi else

write_html_page

fi


We’re not done yet. There are still more things we can do and improvements we can make.


Top OS Cloud Computing at OnWorks: