Saturday, May 9, 2009

shell script

TERM PAPER

LINUX PROGRAMMING
CSE 207



Topic: Write a shell script to display following menu
1. Display List of currently logged in Users
2. Get Help about any Command
3. List the files and sub-directories under some path
4. Reverse a string
5. Quit




Submitted to: Submitted by:
Ms. Shaveta Sharma Mr. Aditya Kandari
Roll.No. - RB1803B57
Section - B1803
Reg.No -10803872


ACKNOWLEDGEMENT


I, express my gratitude towards our subject teacher for the guidelines and help provided by her in making this project a success. She helped me a lot in completing this project.
I would like to say thank you to all those who are involved in this project including my friends. Their valuable inputs in various matter related to the topic helped me a lot.
I have taken the help of some books and websites, listed in references. I would like to thank the library of the university that acted as a database of knowledge for me.
The various sites visited by me on the internet also helped me a lot in making my term paper a success. I thank again one and all.



CONTENTS


 What Is A Linux Shell?
 So What Is A Shell Script
 Why To Write A Shell Script
 The Shell Script
 References Cited


WHAT IS LINUX SHELL ?

Computer understand the language of 0's and 1's called binary language.
In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is pass to kernel.
Shell is a user program or it's environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file.
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Several shell available with Linux including:
Shell Name Developed by Where Remark
BASH ( Bourne-Again SHell ) Brian Fox and Chet Ramey Free Software Foundation Most common shell in Linux. It's Freeware shell.
CSH (C SHell) Bill Joy University of California (For BSD) The C shell's syntax and usage are very similar to
the C programming language.
KSH (Korn SHell) David Korn AT & T Bell Labs --
TCSH See the man page.
Type $ man tcsh -- TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).

So What Is Shell Script?
Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.
Shell script defined as:
"Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file."
Why to Write Shell Script ?
• Shell script can take input from user, file and output them on screen.
• Useful to create our own commands.
• Save lots of time.
• To automate some task of day today life.
• System Administration part can be also automated.




THE SHELL SCRIPT

#!/bin/sh
echo “Main Menu”
echo “1. Display List of currently logged in Users”
echo “2. Get Help about any Command”
echo “3. List the files and sub-directories under some path”
echo “4. Reverse a string”
echo “5. Quit “
read opt
if [
case “$opt” in
1) logged( ); ;
2) help( ); ;
3) list( ); ;
4) rev( ); ;
5) echo “You Have Successfully Quitted From Shell Script”
*) echo “Error - Please Enter A Valid Choice”

exit 0

logged( ) {

#function to print users who are logged in
users=`who | cut -d' ' -f1 | sort -u`
for i in $users
do
echo $i
ps -u $i
done
}

help( ) {
#function to get help on any command
echo –n “Enter the command:-”
read com
echo $man –k $com
}

list( ) {

#function to get list of files and sub-directories
echo –n “Enter path”
read path
echo $ls –al $path
}

rev( ) {
#function to reverse a inputed string and show it.
echo -n "enter the string u want to reverse:-"
read string
len =`echo -n $string |wc -c`
echo "no of character is:- $len"
while test $len -gt 0
do
rev =$rev`echo $string |cut -c $len`
len =`expr $len - 1`
done
echo "the reverse string is:-$rev "
}



REFERENCES CITED

 Begining Linux Programming 4th Edition – Wrox
 Introduction To Linux – Machtelt Garrels
 www.free-os.com
 www.intuitive.com/wicked/showscript

No comments:

Post a Comment