PDA

View Full Version : bash script


ameet
23rd January 2012, 10:09 AM
i am new to bash script and linux ,
using fedora ,

i have to display system information,uptime,drive space by running respective commands
from script ,i have just learned the bash scripting from one link, and they have given one
example also which is shown bellow:


#!/bin/bash

# system_page - A script to produce a 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
{
# Find any release files in /etc

if ls /etc/*release 1>/dev/null 2>&1; then
echo "<h2>System release info</h2>"
echo "<pre>"
for i in /etc/*release; do

# Since we can't be sure of the
# length of the file, only
# display the first line.

head -n 1 $i
done
uname -orp
echo "</pre>"
fi

} # end of system_info


function show_uptime
{
echo "<h2>System uptime</h2>"
echo "<pre>"
uptime
echo "</pre>"

} # end of show_uptime


function drive_space
{
echo "<h2>Filesystem space</h2>"
echo "<pre>"
df
echo "</pre>"

} # end of drive_space

function home_space
{
echo "<h2>Home directory space by user</h2>"
echo "<pre>"
format="%8s%10s%10s %-s\n"
printf "$format" "Dirs" "Files" "Blocks" "Directory"
printf "$format" "----" "-----" "------" "---------"
if [ $(id -u) = "0" ]; then
dir_list="/home/*"
else
dir_list=$HOME
fi
for home_dir in $dir_list; do
total_dirs=$(find $home_dir -type d | wc -l)
total_files=$(find $home_dir -type f | wc -l)
total_blocks=$(du -s $home_dir)
printf "$format" $total_dirs $total_files $total_blocks
done
echo "</pre>"

} # end of home_space

function write_page
{
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_

}

function usage
{
echo "usage: system_page [[[-f file ] [-i]] | [-h]]"
}


##### Main

interactive=
filename=~/system_page.html

while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done


# Test code to verify command line processing

if [ "$interactive" = "1" ]; then
echo "interactive is on"
else
echo "interactive is off"
fi
echo "output file = $filename"



# Write page (comment out until testing is complete)

# write_page > $filename
if [ "$interactive" = "1" ]; then

response=

echo -n "Enter name of output file [$filename] > "
read response
if [ -n "$response" ]; then
filename=$response
fi

if [ -f $filename ]; then
echo -n "Output file exists. Overwrite? (y/n) > "
read response
if [ "$response" != "y" ]; then
echo "Exiting program."
exit 1
fi
fi
fi

#end of script

i have under stood the first four functions system_info,show_uptime,drive_space,home_space
but the further program i didn't got what it is .
any help would be appreciated.

thank you.

sea
23rd January 2012, 01:51 PM
1. Goggle
2. Use [code ] and [/ code], without the spaces...
3. Did you write that or want us to explain it to you?

Keep looking for other pages, more examples.
Dont be afraid of testing.

Start your own script and 'play' with it.
It wont bite, in worst case you crash your system ;)

Some links to get you started:
* http://linuxshellaccount.blogspot.com/2008/01/linux-shell-script-to-gather-basic.html
* http://linuxlibrary.org/command-line/bash-shell-system-information-commands/
* http://tldp.org/

Hope this helps