Pages

Sunday, November 15, 2015

Shell Scripts to See Linux Info and Error Exit




#!/bin/bash
# system_page - A script to produce an system information HTML file
 
##### Constants
 
TITLE="System Information for $HOSTNAME"
RIGHT_NOW=$(date +"%x %r %Z")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
 
##### Functions
 
function system_info
{
echo "<h2>System Information</h2>"
    echo "<pre>"
    uname -a
    echo "</pre>"
}
 
function show_uptime
{
echo "<h2>System uptime</h2>"
    echo "<pre>"
    uptime
    echo "</pre>"
}
 
function drive_space
{
echo "<h2>Filesystem space</h2>"
    echo "<pre>"
    df -h
    echo "</pre>"
}
 
function home_space
{
echo "<h2>Home directory space by user</h2>"
    echo "<pre>"
    echo "Bytes Directory"
    du -s /home/* | sort -nr
    echo "</pre>"
}
 
##### Main
 
cat <<- _EOF_
  <html>
  <head>
      <title>$TITLE</title>
  </head>
 
  <body>
      <h1>$TITLE</h1>
      <p>$TIME_STAMP</p>
      $(system_info)
      $(show_uptime)
      $(drive_space)
      $(home_space)
  </body>
  </html>
_EOF_


===========================
===========================
Error exit function
=========================
#!/bin/bash

# A slicker error handling routine

# I put a variable in my scripts named PROGNAME which
# holds the name of the program being run.  You can get this
# value from the first item on the command line ($0).

PROGNAME=$(basename $0)

function error_exit
{

# ----------------------------------------------------------------
# Function for exit due to fatal program error
#  Accepts 1 argument:
#   string containing descriptive error message
# ----------------------------------------------------------------


 echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
 exit 1
}

# Example call of the error_exit function.  Note the inclusion
# of the LINENO environment variable.  It contains the current
# line number.

echo "Example of error with line number and message"
error_exit "$LINENO: An error has occurred."

=======================================================

No comments:

Post a Comment