39
CSE LAB1 (MASTER Manual) Prepared by: N.KRISHNA Signature of HOD: Department of Computer Science and Engineering

Foss Lab Manual

Embed Size (px)

DESCRIPTION

h

Citation preview

CSE LAB1 (MASTER Manual)

Prepared by: N.KRISHNA Signature of HOD:

Department of Computer Science and Engineering2013-14II Year IInd semCSE LAB List of Conducting Experiments:FOSSs.no Name of the ExperimentPg no

1

Session-1:a)Log into the systemb)Use vi editor to create a file called myfile.txt which contains some text.c)Correct typing errors during creationd)Save the filee)Logout of the system

Session-2:a)Log into the system b)Open the file created in session 1c)Add some textd)Delete some texte)Save the changesg)Logout of the system

2a)Log into the systemb)Use the cat command to create a file containing the following data.Call it mytable use tabs to separate the fields1425 Ravi 15.654320 Ramu 26.276830 Sita 36.151450 Raju 21.86c)Use the cat command to display the file mytable.d)Use the vi command to correct any errors in the file ,mytablee)Use the sort command to sort the file mytable according to the first field. Call the sorted file mytable(same name)f)Print the file mytableg)Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it mytableh)Print the new file mytableg)Logout of the system

s.no NAME OF THE EXPERIMENT PG NO

3

1)a)Login to the system b)Use the appropriate command to determine your login shellc)Use the /etc/passwd file to verify the result of step bd)Use the who command and redirected yhe result to afile myfile1 use more ommand to see the contents of myfile1e)Use the data and who command in sequence(in one line) such that the output of date will display on the screen and the output of who will be redirected to afile called myfile2.Use the more command to check the contents of myfile2

2)a)Write a sed command that deletes the first character in each line in a fileb)Write a sed command that deletes the character before the last character in each line in a filec)Write a sed command that swaps the first and second words in each line in a file

4

a)Pipe your /etc/passwd file to awk, and print out the home directory of each userb)Develop an interactive grep script that asksfor a word and a file name and then tells how many linescontain the word c)Repeatd)Part using awk

5a)Write a shell script that takes acommand-line argument and reports onwhether it is directory,a file,or something elseb)Write a shell script that accepts one or more file name as arguments and converts all of them to uppercase,provided they exists in the current directory

s.no Name of the ExperimentPg no

6

a)Write a shell script that accepts a file name starting and enling line number as arguments and displays all the lines between the given line numbers. b)Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.

7a)write a shell script that computes yhe gross salary of a employee according to the following rules:i. If basic salary is =1500 then HRA=Rs500 and DA=98% of the basicThe basic salary is entered interactively through the key board.b)Write a shell script that accepts two integers as its arguments and computers the value of first number raised to the power of the second number.

8a)Write an interactive file-handling shell program.Let it offer the user the choice of copying,removing,renaming,or linking files. once the user has made a choice,have the program ask the user for the necessary information,such as the file name, new name and so onb)Write a shell script that a login name as command line argument and reports when that person logs in.c)Write a shell script that receieves two file name as arguments.It should check whether the file contents are same or not. If they are same then second file should be deleted.

s.no Name of the ExperimentPg no

9

a)Write a shell script that displays a list of all the files in the current directory to which the user has read,write and executive permissions.

b)Develop an interactive script that ask for a word and a file name and then tells howmany times that word ouccred in the file.

c)Write a shell script to perform the following string operations:i)To extract a sub-String from a given string.ii)To find the length of a given string.

10

Write a C program that tkes one or more file or directory names as command line input and reports the following information on the file:i)File typeii)Number of linksiii)Read,write and execute permissionsiv)Time of last access(note:use stat/fstat system calls)

11

Write C program that simulate the following unix commands:a)mv b)cp (use system calls)

12Write a C program that simulates ls command (use system calls/director

s.no Name of the ExperimentPg no

13Do the following shell programs also1. Write a shell script to check whether a particular user has logged in or not. If he has logged in, also check whether he has eligibility to receive a message or not.2. Write a shell script to accept the name of the file from standard input and perform the following tests on ita. File executableb. File readablec. File writabled. Both readable & writable3. Write a shell script which will display the username and terminal name who login recently in to the unix system.4. Write a shell script to find no. of files in a directory.5. Write a shell script to check whether a given number is perfect or not.6. Write a menu driven shell script to copy, edit, rename and delete a file.7. Write a shell script for concatenation of two strings.8. Write a shell script which will display Fibonacci series up to a given number of argument.9. Write a shell script to accept student number, name, marks in 5 subjects. Find total, average and grade. Display the result of student and store in a file called stu.dat.Rules:avg>=80 then grade AAvg=70 then grade BAvg=60 then grade CAvg=50 then grade DAvg=40 then grade EElse grade F

10. Write a shell script to accept empno,empname,basic. Find DA,HRA,TA,PF using following rules. Display empno, empname, basic, DA,HRA,PF,TA,GROSS SAL and NETSAL. Also store all details in a file called emp.datRules:HRA is 18% of basic if basic > 5000 otherwise 550DA is 35% of basicPF is 13% of basicIT is 14% of basicTA is 10% of basic11. Write a shell script to demonstrate break and continue statements.12. Write a shell script to satisfy the following menu options.a. Display current directory pathb. Display todays datec. Display users who are connected to the unix systemd. Quit13. Write a shell script to delete all files whose size is zero bytes from current directory.14. Write a shell script to display string palindrome from given arguments.15. Write a shell script which will display Armstrong numbers from given given arguments.16. Write a shell script to display reverse numbers from given argument list.17. Write a shell script to display factorial value from given argument list.18. Write a shell script which will find maximum file size in the given argument list.

19. Write a shell script which will greet you Good Morning, Good Afternoon, Good Evening and Good Night according to current time.20. Write a shell script to sort the elements in a array using bubble sort technique.21. Write a shell script to find largest element in a array.22. Write an awk program to print sum, avg of students marks list23. Write an awk program to display students pass/fail report.24. Write an awk program to count the no. of vowels in a given file.25. Write an awk program which will find maximum word and its length in the given input File.26. Write a shell script to generate the mathematical tables.27. Write a shell script to sort elements of given array by using selection sort.28. Write a shell script to search given number using binary search.29. Write a shell script to find number of vowels, consonants, numbers, white spaces and special characters in a given string.30. Write a shell script to lock the terminal

WEEK -1 SESSION-1 a) log into the systemsol) loginb) use vi editor to create a file called myfile.txt which contains some textsol) vi myfilec) correct typing errors during creationsol) practise vi editor commands Esc,id) save the filesol) :wqe) logout the systemsol) logout

WEEK-1 SESSION-2Session-2a) Log into the systemsol:loginb) open the file created in session 1sol: vi myfile.txtc) Add some textsol: i->insert the text before the current cursor location l->insert the text begining of the cursor A->insert the text at the end of the line O->create a newline for entry above cursor location o->create a newline for text entry below the cursor locationd) Change some text sol: k->move the cursor up one line j->move the cursor down one line H->move the cursor left one character L->move the cursor right one character e) Delete some text sol: x->delete the character under the cursor location dw->delete the word dd->delete the line to the cursor is on D->delete of the cursor position to the end of the current line

f) Save the ChangesSol: Practice the commands in Vi editorg) Logout of the systemsol: logout

WEEK-2 a) login to the systemsol: loginb) use the cat command to create a file containing following data.call it my table use tabs to seperate the fields1425Ravi15.654320Ramu26.276830Sita36.171450Raju21.86sol: [13W61A0502 @ localhost ~]$cat>mytable1425Ravi15.654320Ramu26.276830Sita36.151450Raju21.86c) use the cat command to display the file,mytablesol: [13W61A0502 @ localhost ~]$cat>mytable1425Ravi15.654320Ramu26.276830Sita36.151450Raju21.86

d) use the vi command to correct any errors the file,mytablesol: verify the file with vi editor commandse) use the sort command to sort the file mytable according to the first field.call the sorted file mytablesol: [13W61A0502 @ localhost ~]$sort mytable>mytable.txt [13W61A0502 @ localhost ~]$cat mytable.txt1425Ravi15.651450Raju21.864320Ramu26.276830Sita36.15f) print the file mytablesol: [13W61A0502 @ localhost ~]$sort mytable>mytable.txt [13W61A0502 @ localhost ~]$cat mytable.txt1425Ravi15.651450Raju21.864320Ramu26.276830Sita36.15g) use the cut and paste command to swap the fields 2 and 3 of mytablesol: [13W61A0502 @ localhost ~]$cut -f1 mytable>mytable1 [13W61A0502 @ localhost ~]$cat mytable1 1425 4320 6830 1450

[13W61A0502 @ localhost ~]$cut -f2 mytable>mytable2 [13W61A0502 @ localhost ~]$cat mytable2 Ravi Ramu Sita Raju [13W61A0502 @ localhost ~]$cut -f3 mytable>mytable3 [13W61A0502 @ localhost ~]$cat mytable3 15.65 26.27 36.15 21.86 [13W61A0502 @ localhost ~]$paste mytable3 mytable2>mytable4 [13W61A0502 @ localhost ~]$cat mytable4 15.65Ravi 26.27Ramu 36.15Sita 21.86Raju

[13W61A0502 @ localhost ~]$paste mytable1 mytable4>mytable5 [13W61A0502 @ localhost ~]$cat mytable5 142515.65Ravi 432026.27Ramu 683036.15Sita 145021.86Raju

h) print the new file mytablesol: [13W61A0502 @ localhost ~]$cat>myfile.txt Sowjanya50285 Sirisha51380 Viswani50685 Niharika50480

i) logout the systemsol: logout

WEEK-3 SESSION-1

a)Login to the system

b)Use the appropriate command to determine your login shellSol: $echo $SHELLSh

c)Use the /etc/passwd file to verify the result of step b.Sol: $cat /etc/passwd

d)Use the who command and redirect the result to a file called myfile1. Use themore command to see the contents of myfile1.Sol : $who > myfile1 | moreUser1 pts/0 Apr 23 10:43User2 pts/1 May 6 18:19

e)Use the date and who commands in sequence (in one line) such that the outputof date will display on the screen and the output of who will be redirected to a filecalled myfile2. Use the more command to check the contents of myfile2.Sol: $ date ; who > myfile2Fri Aug 9 16:47:32 IST 2008Cat myfile2 :User3 pts/2 Apr 25 10:43User4 pts/3 May 8 18:19

SESSION-2

Input File : file1.dat :Unix is Multiuser OSUnix was developed by Brian Kernighan and KenThomson

a)Write a sed command that deletes the first character in each line in a file.Sol: sed 's/^./ / file1.dat

nix is Multiuser OSnix was developed by Brian Kernighan and KenThomson

b)Write a sed command that deletes the last character in each line in a file.Sol: sed '$s/.$//' file1.dat

Unix is Multiuser OUnix was developed by Brian Kernighan and KenThomso

c)Write a sed command that swaps the first and second words in each line in a file.Sol: sed -e 's/\([^ ]\+\) *\([^ ]\+\)/\2 \1/' sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'

WEEK-4

a)Pipe your /etc/passwd file to awk, and print out the home directory of each user.Sol: cat /etc/passwd | awk { print $7}

b)Develop an interactive grep script that asks for a word and a file name and then tellshow many linescontain that word.Sol:echo Enter a wordread wordecho Enter the filenameread filenol=grep -c $word $fileecho $nol times $word present in the $file

c)Part using awkSol:echo Enter a wordread wordecho Enter the filenameread filenol=awk /$word/ { print NR } Infileecho $nol times $word present in the $file

Week5(a)

AIM:Write a shell script that takes a command line argument and reports on whether it isdirectory, a file, or something else.

Program:

echo " enter file"read strif test -f $strthen echo "file exists n it is an ordinary file"elif test -d $strthen echo "directory file"elseecho "not exists"fiif test -c $strthen echo "character device files"fOutput:[13W61A0506@localhost ~]$ sh week5a.sh viswaniviswani is a directory[13W61A0506@localhost~]$ sh week5a.sh mytablemytable is a file[13W61A0506@localhost~]$ mkdir viswani[13W61A0506@localhost~]$ sh week5a.sh viswaniviswani is a directory

Week-5(b)Aim: Write a shell script that accepts one or more file name as arguments and converts all of them to uppercase, provided they exist in the current directory.

Program:# get filenameecho -n "Enter File Name : " read fileName#make sure file exits for reading if [ ! -f $fileName ] then echo "Filename $fileName does not exists" exit 1 fi #convert uppercase to lowercase using tr command tr '[A-Z]' '[a-z]' < $fileName OUTPUT:[13W61A0506@localhost~] sh week5a.shenter file namefile1 file2[13W61A0506@localhost~] filenameFILE1 FILE2

Week-5(c)Aim:-Write a shell script that determines the period for which a specified user is working on the system.

Program:-echo enter the login of the user read namelogindetails=`who|grep w $name | grep tty if [ $? ne 0 ]thenecho $name has not logged in yet exitfiloginhours=`echo $logindetails | cut c 26,27`loginminuts=`echo $logindetails | cut c 29-30`hoursnow=?date | cut c 12,13`minnow =`date | cut c 15,16`hour=`expr $loginhours - $hoursnow`min=`expr $loginminuts - $minnow`echo $name is working since $hour Hrs - $min MinutsOUTPUT:[13W61A0506@localhost~]$ sh week5c.sh[13W61A0506@localhost~]$ enter the login of the user13W61A0506[13W61A0506@localhost~]$ name has logged in13W61A0506 : x:737:738::/home/13W61A0506:/bin/bash

WEEK-6(A)Aim:-Write a shell script that accepts a file name starting and ending line numbers as arguments and displays all the lines between the given line numbers.Program:-If [ $# -ne 3 ] thenecho chech the arguments once lastline=?wc l < $1?if [ $2 lt $lastline a $3 -le $lastline ] thennline=?expr $3 -$2 + 1?echo ?tail +$2 $1 | head -$nline? elseecho invalid range specification fifiOUTPUT:[13E61A0506@localhost~]$ cat>sm.dathii am finewelcome welcome to sivani[13W61A0506@localhos~] sh week6a.shenter the file namesm.datenter the starting line number 1enter the ending line number

hii am finewelcome

WEEK-6(B)Aim:-Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.Program:-if [ $# -lt 1] thenecho Chech the arguments once exitfiecho Enter a word read wordfor file in $* dogrep iv $word $file | tee 1> /dev/null doneecho lines containing given word are delete"[13W61A0506@localhost~]$ cat sm.datunix is osdos is also oshere using unixunix is powerful os[13W61A0506@localhost~]$ cat sm1.datwindows nt is also osthere are some difference between unix and windows htwindows nt

[13W61A0506@localhost 2cse~]$ cat del.sd{/os/d}

OUTPUT:[13W61A0506@localhos 2cse]$ sed- fdcl.sed sm.dat sm1.dathere using unixthere are some difference between unix and the windows nt[13W61A0506@localhost ~]$ sed -f del.sed sm.dat sm1.dathere using unixunix is powerful os

WEEK-7(A)

Aim:-Write a shell script that computes the gross salary of a employee according to thefollowing rules:i)If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the basic.ii)If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basicThe basic salary is entered interactively through the key board.

Program:-

echo enter basic salaryread sala=0.1b=0.8echo $aecho "hra is"hra=`echo 0.1 \* $sal|bc`echo da isda=`echo 0.8\*$sal|bc`gsal=expr $hra + $da + $salecho $gsal

Output:-

WEEK-7(B)

Aim:-Write a shell script that accepts two integers as its arguments and computers the valueof first number raised to the power of the second number.

Program:-

If [ $# -ne 2 ]thenecho chech the number of argumentscount=1result=1if [ $2 ge 0 ]thenwhile [ $count le $2 ]doresult=`expr $result \* $1`count=`expr $count + 1`donefifi

output:-

WEEK-8(A)Aim:- a)Write an interactive file-handling shell program. Let it offer the user the choice ofcopying, removing, renaming, or linking files. Once the user has made a choice, have theprogram ask the user for the necessary information, such as the file name, new name andso on.b)Write shell script that takes a login name as command line argument and reportswhen that person logs inSol:#Shell script that takes loginname as command line arg and reports when that person logsin.if [ $# -lt 1 ]thenecho improper usageecho correct usage is: $0 usernameexitfilogname=$1while truedowho|grep "$logname">/dev/nullif [ $? = 0 ]thenecho $logname has logged inecho "$logname">>sh01log.txtdate >>sh01log.txtecho "Hi" > mesg.txtecho "$logname" >> mesg.txtecho "Have a Good Day" >> mesg.txtmail "$logname" < mesg.txtexitelsesleep 60 fi done

WEEK8(C)Aim:-Write a shell script which receives two file names as arguments. It should checkwhether the two file contents are same or not. If they are same then second file should bedeleted.

Program:-

echo enter first file nameread file1echo enter second file nameread file2cmp file1 file2 > file3if [ -z $file1 ] rm file2fiecho duplicate file deleted successfullyoutput

WEEK-9

WEEK-10Aim:-Write a C program that takes one or more file or directory names as command line inputand reports the following information on the file:i)File typeii)Number of linksiii)Read, write and execute permissionsiv)Time of last access

PROGRAM:-

#include#include#include#include#includevoid main(){ int fd;struct stat buf;fd=open("f5.txt",O_RDONLY|O_CREAT,600);if(fd!=-1){ if(fstat(fd,&buf)==0){ printf("mode of fileis %u",buf.st_mode);printf("\n size of the file is %u",buf.st_size);printf("\n device name %u",buf.st_dev);printf("\n inode of file is %u",buf.st_ino);printf("\n no. of links are %u",buf.st_nlink);printf("\n owner oof a file is %u",buf.st_uid);printf("\n no.of blocks is %u",buf.st_blocks);printf("\n group owner is %u",buf.st_gid);printf("\n blocks size of the file is %u",buf.st_blksize);printf("\n time of last modifiedis %u",buf.st_ctime);}elseprintf("error in fstat() syscall");}elseprintf("error in open() sys call");}

WEEK-11

AIM:- Write C programs that simulate the following unix commands:a)mv

Program:-

#include#include#include#include#includevoid main(){int fd1,fd2;char buf[60];char *p="/f2";fd1=open("f2",O_RDWR);fd2=open("f6",O_RDWR);read(fd1,buf,sizeof(buf));write(fd2,buf,sizeof(buf));remove(p);}

b)cp#include#include#include#include#includevoid main(){int fd1,fd2;char buf[60];fd1=open("f2",O_RDWR);fd2=open("f6",O_RDWR);read(fd1,buf,sizeof(buf));write(fd2,buf,sizeof(buf));close(fd1);close(fd2);}

WEEK-12Aim:-Write a C program that simulates ls Command

Program:-

#include#include#include#include#include#includevoid main(){DIR *dp;struct dirent *dirp;dp=opendir(".");while(dirp=(readdir(dp)!=NULL)){if(dirp->d_ino==0)continue;elseprintf("%s \n",dirp->d_name);}}