unix

38
INDEX S.N o Date Name of the Experiment Page No Sign of the Staff 1. MENU DRIVEN PROGRAM 2. FILE MANUPULATION 3. PATTERN SEARCHING 4. SUM OF THE SERIES 5. SORTING 6. EB BILL PREPARATION 7. FILE PERMISSIONS 8. PAY SLIPPREPARATION 9. PATIENT DETAILS

Upload: smileflower

Post on 23-Nov-2014

578 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Unix

INDEX

S.No Date Name of the Experiment Page No Sign of the Staff

1. MENU DRIVEN PROGRAM

2. FILE MANUPULATION

3. PATTERN SEARCHING

4. SUM OF THE SERIES

5. SORTING

6. EB BILL PREPARATION

Page 2: Unix

7. FILE PERMISSIONS

8. PAY SLIPPREPARATION

9. PATIENT DETAILS

Aim:

To write a menu driven shell program to display the list of files,processes of users,today’s date and users of system.

Alogorithm:

Start the program.

Using read command get the choice from the user.

If choice=1 then display the list of files in a directory and information about them.

Expt.No.1 MENU DRIVEN SHELL PROGRAM

Page 3: Unix

If choice=2 then display the information about the processes of users.

If choice=3 then it display the current date.

If choice=4 then it shows the information about the users on the system and their terminal ID number.

If choice-5 then terminate the program.

Program:

echo “ MENU

1. List of Files

2. Processes of Users

3. Today’s Date

4. Users of System

5. Quit to UNIX

Enter Your option:”

read choice

case $choice in

Page 4: Unix

1) ls -l ;;

2) ps -f ;;

3) date ;;

4) who ;;

*) exit

esac

Result:

MENU

1) List of files

2) Processes of users

3) Today’s Date

4) Users of System

5) Quit to UNIX

Input:

Enter your Option: 1

Output:

08_packets.html

Page 5: Unix

TOC.sh

cptodos.sh

dept.lst

Input:

Enter your option: 3

Output:

Sun Oct 01 11:17:09 IST 2006

*-----------*-----------*

Expt.No.2 FILE MANIPULATION

Aim:

To write a menu driven shell program to copy,edit,rename and delete a file.

Algorithm:

Start the program

Read a filename as “fn”

Get the choice as “ch”

Check thet the file “fn” is already exists or not by –e operator

Page 6: Unix

If ch=1 then copy the content of file fn into a new filename as “new”

If ch=2 then edit the file fn with vi editor

If ch=3 then rename the file fn with a new filename as mov

If ch=4 then remove the file fn from the directory

If ch=5 then terminate the program

Program:

# Menu driven shell program to copy,edit,rename,delete a file

ch=0

while [ $ch –ne 5 ]

do

echo 1. Copy a file

echo 2. Edit a file

echo 3. Rename a file

echo 4. Delete a file

echo 5. Exit

echo Enter your choice [1….5]

read ch

echo Enter a file name

read fn

Page 7: Unix

if ! [ -e $ fn ]

then

echo The file $fn does not exist

exit

fi

case $ch in

1) cp $fn New;;2) vi $fn;;3) mv $fn mov;;4) rm $fn;;*) exit

esac

done

Result:

1. Copy a file

2. Edit a file

3. Rename a file

4. Delete a file

5. Exit

Input:

Page 8: Unix

Enter your choice[1….5]:1

Output:

Enter a file name:old

The file old is copied with the filename New

*-----------*-----------*

Expt.No.3 PATTERN SEARCHING

Aim:

To write a shell program using 3 arguments to take the pattern as well as input and output filenames.If the pattern is found,display “Pattern found” else display error message.also check if right numbers of arguments are entered.

Algorithm:

Start the program

Read the source filename as “sf”

Page 9: Unix

Using –e operator check whether the file is already exists.If not print that the file is missing.

Read the target filename as “tf” and the pattern as “pat”

The grep command searches the given pattern through the source file and prints all the matching lines into the target file.

Program:

# Shell program using 3 arguments to take the pattern as well as

# input and output file names.If the pattern is found,display

#”Error Message”.Also check if right number of arguments are

# entered

echo Enter the Source filename

reda sf

if ! [ -e $sf ]

then

echo The file $sf missing

exit

Page 10: Unix

fi

echo Enter the Traget filename

read tf

echo Enter the pattern

read pat

grep $pat $sf > $tf

cat $tf

Result:

Input:

Enter the Source filename: employee.lst

Enter the Target filename: direct.lst

Enter the pattern: director

Output:

2365 | barun sengupta |director |personnel |7800

Page 11: Unix

9876 | jai Sharma |director |production |7000

*-----------*-----------*

Expt.No.4 SUM OF THE SERIES

Aim:

To write a shell program to sum up the series

1/1! + ½! + 1/3!+……………+1/10!

Algorithm:

Start the program

Page 12: Unix

Assume s==0 and f=1

In for loop statement give the numbers from(1…..10)in a variable i

Then calculate f=f*l and s=s+1/f

Print the value of s

Terminate the program

Program:

# shell program to sum up the series

# 1 1 1 1

# ---- + ---- + ---- + …. + ----

# 1! 2! 3! 10!

clear

echo sum of series

echo ~~~~~~~~~~~~

s=0

Page 13: Unix

f=1

for I in 1 2 3 4 5 6 7 8 9 10

do

f= ’ expr $f \ * $i’

s=’ expr $s + 1 / $f’

done

echo sum of 10 terms = $s

Result:

Output:

Sum of series~~~~~~~~~~~~

Sum of 10 terms = 1.5

*-----------*-----------*

Page 14: Unix

Expt.No.5 SORTING THE FILE CONTENTS

Aim:

To write a shell program accepts the names of a file from the standard input and then perform the tests like entering the names in a file,sorting the names in existing file,listing the sorted and unsorted file.

Algorithm:

Start the program

Page 15: Unix

Read a filename to store the names to be sorted as ‘fn’

Read any five names and assign it in variable ‘name’

Using sort command,sort the given filenames.

Then display the sorted and unsorted list by cat command

Terminate the program.

Program:

# shell program which accepts the name of the file form the#standard input and then perform the following tests on it:# 1. Enter the 5 names in the file# 2. Sort the name in existing file# 3. List unsorted and sorted file# 4. Quit

echo Enter a filename to store the names to be sorted

read fn

echo Enter five names

for I in 1 2 3 4 5

do

Page 16: Unix

read name

echo $name >> $fn

done

sort $ fn > sortfile

echo Unsorted List

echo ~~~~~~~~~~~~

cat $fn

echo Sorted List

echo ~~~~~~~~~~

cat Sortfile

Result:

Input:

Enter a filename to store the names to be sorted: unsorted

Enter five names

Kumar

Sharma

Rayan

Muthu

Arun

Output:

Unsorted list

Page 17: Unix

~~~~~~~~~~~

Kumar

Sharma

Rayan

Muthu

Arun

sorted list~~~~~~~~~

Arun

Kumar

Muthu

Rayan

Sharma

*-----------*-----------*

Expt.No.6 EB BILL PREPARATION

Aim:

To write a shell program to prepare electricity bill for domestic customers

Algorithm:

Start the program.

Read the customer number,name,previous reading and current reading.

Calculate the units consumed by subtracting the current reading by previous reading.

Page 18: Unix

Assume for first100 units as Rs.0.75 per unit

Assume for next100 units as Rs.1.50 per unit

Assume for above200 units as Rs.3.00 per unit

Check if unit > 200 then calculate temp = unit – 200 and charge = 225 + temp * above200

Check if unit > 200 then calculate temp = unit – 200 and charge = 225 + temp * above200

Else Check if unit > 100 then calculate temp = unit – 100 and charge = 75 + temp * next100

Else calculate charge = unit * first100

Print the details and terminate the program

Program:

# Shell program to prepare electric bill for customers

# For first 100 units - Rs. 0.75/unit

# For next 100 units - Rs. 1.50/unit

# Above 200 units - Rs. 3.00/unit

Clear

echo -n “Enter Customer No: “

Page 19: Unix

read no

echo -n “Enter Customer Name: “

read name

echo -n “Enter Previous reading: “

read prev

echo -n “Enter Current reading: “

read curr

unit=’expr $curr - $prev’

first100=’expr 3 / 4’

next100=’expr 3 / 2’

above200=3

if [ $unit -gt 200 ]

then

temp=’expr $unit - 200’

charge=’expr 225 + $temp \ * $above200’

elif [ $unit -gt 100]

then

temp=’expr $unit - 100’

charge=’expr 75 + $temp \ * $next100’

else

Page 20: Unix

charge=’expr $unit \ * $first100’

fi

clear

echo “##############################################################”

echo Electricity Billecho ~~~~~~~~~~~~

echo Customer No : $no

echo Customer Name : $name

echo Previous Reading : $prev

echo Current Reading : $curr

echo Units : $unit

echo Charge : $charge

echo “##############################################################”

Result:

Input:

Enter Customer No :5347

Enter Customer Name :Karthick

Enter Previous reading :150

Enter Current reading :250

Output:

Page 21: Unix

##############################################################

Electricity Bill~~~~~~~~~~~~

Customer No :5347

Customer Name :Karthick

Previous Reading :150

Current Reading :250

Units :100

Charge :75

##############################################################

*-----------*-----------*

Expt.No.7 CHECKING FILE PERMISSIONS

Aim:

To write a shell program which accepts the name of a file from the standard input and then to perform the tests like file existence, readable, writable and both readable & writable.

Algorithm:

Start the program

Page 22: Unix

Read a filename in variable fn

Using -e operator check whether the file is already exists or not

The -r operator is used to check that the file is readable

Using -w operator check that the file is writable

The -r and -w operators are used to check that the file is readable and writable

Terminate the program

Program:

# shell program which accepts the name of a file from the

# standard input and then perform the following tests on it:

# 1. File Existance

# 2. File Readable

# 3. File writable

# 4. Both Readable and Writable

Page 23: Unix

echo Enter a filename

read fn

if [ -e $fn ]

then

echo The file $fn exists

else

echo The file $fn does not exist

exit

fi

if [ -r $fn ]

then

echo The file $fn is readable

else

echo The file $fn is not readable

fi

if [ -w $fn ]

Page 24: Unix

then

echo The file $fn is writable

else

echo The file $fn is not writable

fi

if [-r $fn ] && [ -w $fn ]

then

echo The file $fn is readable and writable

else

echo The file $fn is not readable and writable

fi

Result:

Input:

Enter a filename: employee.lst

Output:

The file employee.lst exists

Page 25: Unix

Input:

Enter a filename: employee.lst

Output:

The file employee.lst is readable and writable

*-----------*-----------*

Expt.No.8 PAY SLIP PREPARATION

Aim:

To write a shell program to print monthly pay slip for employees in an organization.

Algorithm:

Start the program

Read the name, month and basicpay of the employee in the variables $name, $month and $basic respectively.

Calculate 29% of DA from $basic and store it into $da

Page 26: Unix

Calculate 5% of HRA from $basic and store it into $hra

Calculate 2% of MA from $basic and store it into $ma

Calculate Gross by adding $basic, $da, $hra and $ma

Calculate 10% of PF from $basic and store it into $pf

Calculate 2% of IT from $gross and store it into $it

Calculate total deductions by adding $pf and $it

Calculate Net salary by subtraction $net from $gross

Print all the data in the prescribed format

Terminate the program

Program:

# shell program to print monthly payslips

# for employees in organization

clear

echo -n “Enter the Employee Name: “

read name

echo -n “Enter the Salary month :”

read month

echo -n “Enter the basic pay :”

Page 27: Unix

read basic

da= ‘expr $basic \*29 /100’ # da is 29% on basic

hra=‘expr $basic \*5 /100’ #hra is 5% on basic

ma= ‘expr $basic \*2 /100’ # ma is 2% on basic

gross= ‘expr $basic + $da + $hra + $ma’

pf= ‘expr $basic \*10 /100’ # pf is 10% on basic

it= ‘expr $gross \*2 /100’ # it is 2% on gross

ded= ‘expr $pf + $it’

net== ‘expr $gross - $ ded’

clear

echo===================================================

echo” Nmae : $name”

echo” PAY SLIP FOR THE MONTH OF $month”

echo===================================================

echo” DUES DEDUCTIONS”

echo”Basic pay - $basic Provident Fund - $pf

Page 28: Unix

echo”DA - $basic Income Tax - $it

echo”HRA - $hra”

echo”MA - $ma”

echo” -------------- --------------

echo”Gross - $gross Total Deduction - $ded”

echo” -------------- --------------

echo”Net Salary - $net”

echo===================================================

Result:

Input:

Enter the Employee Name: Sudha

Enter the Salary month: November2006

Enter the basic pay: 4200

Output:

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

Name : Sudha

PAY SLIP FOR THE MONTH OF November 2006

Page 29: Unix

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

DUES DEDUCTIONS

Basic pay - 4200 Provident Fund - 420

DA - 1218 Income Tax - 114

HRA - 210

MA - 84

-------------- --------------

Gross - 5712 Total Deduction - 534

-------------- --------------

Net Salary - 5178

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

*-----------*-----------*

Expt.No.9 PATIENT DETAILS

Aim:

To write a shell program to maintain the patient details in a hospital.

Algorithm:

Page 30: Unix

Start the program

Read the option from the user into $opt

If $opt=1 then read the type of the blood group in $bg and list all the patients who have the blood group using grep command

If $opt=2 then list all the patients who’s age lies between 20 and 30

Terminate the program

Program:

# shell program to maintain patient details

echo “ Patient Maintenance”

echo 1. Patients in blood groupwise

echo 2. Patients in age between 20 and 30

echo 3. Exit

echo -n “Enter your option: “

read opt

Page 31: Unix

case $opt in

1) Echo -n “Enter the blood group: “

Read bg

grep $bg patients.txt ;;

2) Grep 2[0-9] patients.txt ;;

*) exit ;;

esac

Result:

Patient Maintenance

1.Patients in blood groupwise2.Patients in age between 20 and 303.Exit

Input:

Enter your option:1Enter the bloodgroup: A1B+

Output:

5041 V.NAGESWARI FEMALE 48 A1B+ VIRUDHUNAGAR

Page 32: Unix

Result:

Patient Maintenance

1.Patients in blood groupwise2.Patients in age between 20 and 303.Exit

Input:

Enter your option:2

Output:

5044 RAJASEKARAN MALE 24 A- THIRUMANGALAM5046 SITALAKSHMI FEMALE 28 A+ MADURAI5022 ARUMUGAM MALE 21 B+ VIRUDHUNAGAR5029 NATARAJAN MALE 26 O+ RAM NAGAR5030 SANKAR MALE 29 O- THIRUNAGARAM

*-----------*-----------*