Fedora Linux Support Community & Resources Center
  #1  
Old 23rd January 2012, 10:09 AM
ameet Offline
Registered User
 
Join Date: Jan 2012
Location: india
Posts: 6
windows_xp_2003chrome
bash script

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.
Reply With Quote
  #2  
Old 23rd January 2012, 01:51 PM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Helvetic Federation (Swissh)
Age: 33
Posts: 2,608
linuxfirefox
Re: bash script

1. Goggle
Code:
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.co...her-basic.html
* http://linuxlibrary.org/command-line...tion-commands/
* http://tldp.org/

Hope this helps
__________________
Fedora Manual: http://docs.fedoraproject.org
Script-Tools: https://sourceforge.net/projects/script-tools/
sudo st tweak repo toggle fedora-rawhide ; st iso dl-fed -respin && st iso usb

Last edited by sea; 23rd January 2012 at 01:53 PM.
Reply With Quote
Reply

Tags
bash, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with bash script woodson2 Programming & Packaging 5 25th December 2010 02:09 PM
Convert bash script to perl script homey Programming & Packaging 1 2nd September 2006 04:24 AM


Current GMT-time: 08:05 (Sunday, 26-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat