roll linux)

106
Roll Number:-33 Computer Science Paper No:-3 Practical Practical No:-1 (a) Write and execute the commands for the following: i. Create a file item1<rollno> with atleast three lines. Copy the file into file item2<rollno>. ii. Count number of lines, words & characters in a file item1<seatno>. Solution:- i. [user37@centosserver ~]$ cat > item1 1 2 3 4 [user37@centosserver ~]$ cp item1 item2 [user37@centosserver ~]$ cat item2 1 2 3 4 ii. [user37@centosserver ~]$ wc -l item1 4 item1 1

Upload: sushant-shinde

Post on 05-Sep-2014

196 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Roll Linux)

Roll Number:-33

Computer Science Paper No:-3 Practical

Practical No:-1

(a) Write and execute the commands for the following:

i. Create a file item1<rollno> with atleast three lines. Copy the file into file

item2<rollno>.

ii. Count number of lines, words & characters in a file item1<seatno>.

Solution:-

i.

[user37@centosserver ~]$ cat > item1

1

2

3

4

[user37@centosserver ~]$ cp item1 item2

[user37@centosserver ~]$ cat item2

1

2

3

4

ii.

[user37@centosserver ~]$ wc -l item1

4 item1

[user37@centosserver ~]$ wc -w item1

4 item1

[user37@centosserver ~]$ wc -c item1

8 item1

1

Page 2: Roll Linux)

Roll Number:-33

(b) Write and execute the commands to change the directory to /bin and do the

following:

i. list all filenames.

ii. list all filenames with one screen at a time.

iii. list all filenames with 2 characters, 3 characters.

iv. List all filenames starting with vowel.

v. List all filenames with the last character as a or b or c or d.

vi. List all filenames with exactly three characters in which the second

character is a vowel.

Solution:-

i.

[user37@centosserver ~]$ cd /bin

[user37@centosserver bin]$ ls

alsacard ed mailx sleep

alsaunmute egrep mkdir sort

arch env mknod stty

awk ex mktemp su

basename false more sync

bash fgrep mount tar

cat fipscheck mountpoint taskset

chgrp fipshmac mv tcptraceroute

chmod gawk netstat tcsh

chown gettext nice touch

ii.

[user37@centosserver bin]$ ls | more

alsacard

alsaunmute

arch

awk

basename

2

Page 3: Roll Linux)

Roll Number:-33

bash

cat

chgrp

chmod

fgrep

iii.

[user37@centosserver bin]$ ls ??

cp dd df ed ex ln ls mv ps rm sh su vi

[user37@centosserver bin]$ ls ???

awk cat csh cut env ksh pwd raw red rpm rvi sed tar zsh

iv.

[user37@centosserver bin]$ ls [aeiou]*

alsacard awk egrep igawk uname unlink

alsaunmute echo env ipcalc unicode_start usleep

arch ed ex umount unicode_stop

v.

[user37@centosserver bin]$ ls *[abcd]

alsacard dbus-send doexec fipshmac loadkeys.static pwd sed

chmod dd ed ipcalc mknod red sync

vi.

[user37@centosserver bin]$ ls ?[aeiou]?

cat cut raw red sed tar

(c) Write and execute the commands to change the directory to /etc and do the

following:

i. List the contents of the directory.

ii. List the contents of the directory along with all hidden files.

iii. List all files with their attributes and file permissions.

3

Page 4: Roll Linux)

Roll Number:-33

iv. List all files identifying directories and executable files.

v. Give the file listing displayed in columns.

vi. Give the file listing in reverse order.

Solution:-

i.

[user37@centosserver etc]$ ls

a2ps.cfg gimp modprobe.d rpm

a2ps-site.cfg gnome-vfs-2.0 motd rwtab

acpi gnome-vfs-mime-magic mtab rwtab.d

adjtime gpm-root.conf mtools.conf samba

alchemist gre.d multipath.conf sane.d

[user37@centosserver etc]$ ls

a2ps.cfg gimp modprobe.d rpm

a2ps-site.cfg gnome-vfs-2.0 motd rwtab

acpi gnome-vfs-mime-magic mtab rwtab.d

adjtime gpm-root.conf mtools.conf samba

alchemist gre.d multipath.conf sane.d

ii.

[user37@centosserver etc]$ ls -aFx

./ ../ a2ps.cfg

a2ps-site.cfg acpi/ adjtime

alchemist/ aliases aliases.db

alsa/ alternatives/ anacrontab

asound.state at.deny audisp/

audit/ autofs_ldap_auth.conf auto.master

auto.misc auto.net* auto.smb*

avahi/ bashrc blkid/

bluetooth/ bonobo-activation/ capi.conf

iii.

[user37@centosserver etc]$ ls -l

total 3788

4

Page 5: Roll Linux)

Roll Number:-33

-rw-r--r-- 1 root root 15288 May 24 2008 a2ps.cfg

-rw-r--r-- 1 root root 2562 May 24 2008 a2ps-site.cfg

drwxr-xr-x 4 root root 4096 Mar 12 2010 acpi

-rw-r--r-- 1 root root 44 Nov 26 16:27 adjtime

drwxr-xr-x 4 root root 4096 Mar 12 2010 alchemist

-rw-r--r-- 1 root root 1512 Apr 25 2005 aliases

-rw-r----- 1 root smmsp 12288 Nov 29 14:06 aliases.db

iv.

[user15@centosserver etc]$ ls -f

. dumpdates nsswitch.conf

.. esd.conf security

Muttrc sudoers man.config

resolv.conf securetty profile

zshrc bonobo-activation inputrc

webalizer.conf a2ps-site.cfg hosts

minicom.users rc0.d initlog.conf

rpc cron.deny zlogout

passwd adjtime crontab

php.ini openldap desktop-profiles

v.

[user37@centosserver etc]$ ls -x

a2ps.cfg a2ps-site.cfg acpi

adjtime alchemist aliases

aliases.db alsa alternatives

anacrontab asound.state at.deny

audisp audit autofs_ldap_auth.conf

vi.

[user15@centosserver etc]$ ls -r

zshrc rhgb mke2fs.conf gconf

zshenv resolv.conf minicom.users fstab

zprofile redhat-release mime.types foomatic

5

Page 6: Roll Linux)

Roll Number:-33

zlogout redhat-lsb mgetty+sendfax fonts

zlogin reader.conf.d maven firmware

yum.repos.d reader.conf man.config filesystems

yum.conf readahead.d makedev.d fb.modes

yum rc.sysinit mail.rc exports

yp.conf rc.local mailcap esd.conf

(d) Write and execute the commands for the following:

i. Display /usr/sbin one screen at a time

ii. Display /usr/sbin beginning with a lowercase ‘c’.

iii. Display and count all the lines in the file /etc/mime.types

iv. To find out how many files are in the /usr/bin directory.

Solution:-

i.

[user15@centosserver etc]$ cd /usr/sbin

[user15@centosserver sbin]$ ls|more

accept

accton

acpid

adduser

adsl-connect

adsl-setup

adsl-start

adsl-status

adsl-stop

alsactl

alternatives

anacron

apachectl

apmd

arpd

6

Page 7: Roll Linux)

Roll Number:-33

arping

atd

atrun

audit2why

authconfig

authconfig-gtk

authconfig-tui

automount

avahi-autoipd

--More—

ii.

[user15@centosserver sbin]$ ls c*

cacertdir_rehash chpasswd cpuspeed cupsctl

callback chroot cracklib-check cupsd

capiinit cifs.upcall cracklib-format cupsdisable

cc_dump clockdiff cracklib-packer cupsenable

ccreds_validate conmand cracklib-unpacker cupsfilter

cc_test convertquota create-cracklib-dict

chat cossdump crond

chkfontpath cpufreq-selector cupsaddsmb

iii.

[user15@centosserver etc]$ cd /etc

[user15@centosserver etc]$ ls mime.types

mime.types

[user15@centosserver etc]$ wc -l mime.types

516 mime.types

iv.

[user15@centosserver bin]$ cd /usr/bin

[user15@centosserver bin]$ ls | wc -w

2132

7

Page 8: Roll Linux)

Roll Number:-33

(e) Write and execute the commands for the following:

i. Display the last 10 lines of /etc/mime.types

ii. Display the first 25 lines of /etc/mime.types

iii. Create a file named fsp<seat_no> having the listing of atleast 50 lines (e.g,

listing of /usr/sbin or /usr/bin or /etc or can create your own).

iv. Display first 2 lines of fsp<seat_no> and convert all the characters into

capital letters.

v. Display the last 15 lines of fsp<seat_no>.

vi. Split the file fsp<seat_no> into subparts each having at most 20 lines and

display the contents of these subparts and count the number of lines in

them.

vii. Split the file fsp<seat_no> into three subparts named fspaa, fspab, fspac

and display the contents of these files and count the number of lines in

them.

Solution:-

i.

[user15@centosserver etc]$ tail -10 /etc/mime.types

video/vnd.vivo

video/x-flv flv

video/x-ms-asf asf asx

video/x-ms-wm wm

video/x-ms-wmv wmv

video/x-ms-wmx wmx

video/x-ms-wvx wvx

video/x-msvideo avi

video/x-sgi-movie movie

x-conference/x-cooltalk ice

8

Page 9: Roll Linux)

Roll Number:-33

ii.

# This is a comment. I love comments.

# This file controls what Internet media types are sent to the client for

# given file extension(s). Sending the correct media type to the client

# is important so they know how to handle the content of the file.

# Extra types can either be added here or by using an AddType directive

# in your config files. For more information about Internet media types,

# please read RFC 2045, 2046, 2047, 2048, and 2077. The Internet media type

# registry is at <http://www.iana.org/assignments/media-types/>.

# MIME type Extension

application/EDI-Consent

application/EDI-X12

application/EDIFACT

application/activemessage

application/andrew-inset ez

application/applefile

application/atomicmail

application/batch-SMTP

application/beep+xml

application/cals-1840

application/commonground

application/cybercash

application/dca-rft

application/dec-dx

iii.

[user37@centosserver sheetal]$ cat > fsp0

# This is a comment. I love comments.

# This file controls what Internet media types are sent to the client for

# given file extension(s). Sending the correct media type to the client

# is important so they know how to handle the content of the file.

# Extra types can either be added here or by using an AddType directive

# in your config files. For more information about Internet media types,

9

Page 10: Roll Linux)

Roll Number:-33

# please read RFC 2045, 2046, 2047, 2048, and 2077. The Internet media ty

# registry is at <http://www.iana.org/assignments/media-types/>.

# MIME type Extension

application/EDI-Consent

application/EDI-X12

application/EDIFACT

application/activemessage

iv.

[user37@centosserver sheetal]$ head -2 fsp0

# This is a comment. I love comments.

v.

[user37@centosserver sheetal]$ tail -15 fsp0

application/index.vnd

application/iotp

application/ipp

application/isup

application/font-tdpfr

vi.

[user37@centosserver sheetal]$ split -20 fsp0

[user37@centosserver sheetal]$ ls

fsp0 xaa xab xac

[user37@centosserver sheetal]$ cat xaa

# This is a comment. I love comments.

# This file controls what Internet media types are sent to the client for

# given file extension(s). Sending the correct media type to the client

# is important so they know how to handle the content of the file.

# Extra types can either be added here or by using an AddType directive

# in your config files. For more information about Internet media types,

# please read RFC 2045, 2046, 2047, 2048, and 2077. The Internet media type

# registry is at <http://www.iana.org/assignments/media-types/>.

10

Page 11: Roll Linux)

Roll Number:-33

# MIME type Extension

application/EDI-Consent

application/EDI-X12

application/EDIFACT

application/activemessage

application/andrew-inset ez

application/applefile

[user37@centosserver sheetal]$ cat xab

application/cals-1840

application/commonground

application/cybercash

application/dca-rft

application/dec-dx

application/dvcs

[user37@centosserver sheetal]$ cat xac

application/mac-compactpro cpt

application/macwriteii

application/marc

application/mathematica

[user37@centosserver sheetal]$ wc -l xaa

20 fspaa

[user37@centosserver sheetal]$ wc -l xab

20 fspab

[user37@centosserver sheetal]$ wc -l xac

9 fspac

vii.

[user37@centosserver sheetal]$ split -20 fsp0 fsp

[user37@centosserver sheetal]$ ls

fsp0 fspaa fspab fspac xaa xab xac

[user37@centosserver sheetal]$ cat fspaa

11

Page 12: Roll Linux)

Roll Number:-33

# This is a comment. I love comments.

# This file controls what Internet media types are sent to the client for

# given file extension(s). Sending the correct media type to the client

# is important so they know how to handle the content of the file.

# Extra types can either be added here or by using an AddType directive

# in your config files. For more information about Internet media types,

# please read RFC 2045, 2046, 2047, 2048, and 2077. The Internet media type

# registry is at <http://www.iana.org/assignments/media-types/>.

# MIME type Extension

application/EDI-Consent

application/EDI-X12

application/EDIFACT

application/activemessage

application/andrew-inset ez

application/applefile

[user37@centosserver sheetal]$ cat fspab

application/cals-1840

application/commonground

application/cybercash

application/dca-rft

application/dec-dx

application/dvcs

[user37@centosserver sheetal]$ cat fspac

application/mac-compactpro cpt

application/macwriteii

application/marc

application/mathematica

[user37@centosserver sheetal]$ wc -l fspaa

20 fspaa

[user37@centosserver sheetal]$ wc -l fspab

20 fspab

12

Page 13: Roll Linux)

Roll Number:-33

[user37@centosserver sheetal]$ wc -l fspac

9 fspac

13

Page 14: Roll Linux)

Roll Number:-33

Practical 2

Part I :

a) Write and execute commands to create a file named name<roll_no> with fields

(first name, second name, last name, salary) separated by “ : ”.Insert at least five

appropriate records in above file. Perform following sort operations:

i. Sort on first names only.

ii. Display only those records whose first names start with a vowel.

iii. Sort on last names only.

iv. Display the names with salary above 10000 and and add two more records and

redirect the output to the file named namenew<roll_no>.

Solution:-

[user37@centosserver ~]$ cat > sheetal37

prachi:uttam:pawar:10000

sheetal:kishor:yeola:20000

sandesh:dilip:bachim:30000

chetan:yuvraj:kamble:40000

abhi:krishna:shete:50000

i.

[user15@centosserver ~]$ sort -t ":" +0 -1 sheetal37

abhi:krishna:shete:50000

chetan:yuveraj:kamble:40000

prachi:uttam:pawar:10000

sandesh:dilip:bachim:30000

sheetal:kishor:yeola:20000

ii.

[user37@centosserver ~]$ grep -i ^[aeiou] sheetal37

abhi:krishna:shete:60000

14

Page 15: Roll Linux)

Roll Number:-33

iii.

[user15@centosserver ~]$ sort -t ":" +2 -3 sheetal37

sandesh:dilip:bachim:30000

chetan:yuveraj:kamble:40000

prachi:uttam:pawar:10000

abhi:krishna:shete:50000

sheetal:kishor:yeola:20000

iv.

b) Write and execute the commands to create a file with the name

Stud<roll_no> with the following fields separated by a blank space having the

below mentioned values:

Field RollNo First Name Last Name Date of Birth Marks

Values Numeric Character Character dd-mm-yy Numeric out

of 600

i. Insert at least five appropriate records and do the following:

ii. Sort the data on first names only.

iii. Sort the data on the Marks only

iv. Prepare a ranked merit list with student’s first and last name only and store

in the file Merit<roll_no> and display its contents.

Solution:-

i.

[user15@centosserver ~]$ cat > stud15

15 rajeshri kajrolkar 5/5/1992 500

07 komal dhere 14/11/1991 510

10 trupti gawade 2/10/1990 520

24 priyanka patil 5/7/1991 545

04 nayantara chaugule 31/5/1991 550

15

Page 16: Roll Linux)

Roll Number:-33

ii.

[user15@centosserver ~]$ sort -t" " +1 -2 stud15

07 komal dhere 14/11/1991 510

04 nayantara chaugule 31/5/1991 550

24 priyanka patil 5/7/1991 545

15 rajeshri kajrolkar 5/5/1992 500

10 trupti gawade 2/10/1990 520

iii.

[user15@centosserver ~]$ sort -t 5 stud15

04 nayantara chaugule 31/5/1991 550

24 priyanka patil 5/7/1991 545

10 trupti gawade 2/10/1990 520

07 komal dhere 14/11/1991 510

15 rajeshri kajrolkar 5/5/1992 500

iv.

c) Write and execute commands for the following:

Create a file item1<seatno> with following fields having the mentioned type of

values

Item code Item name Unit of measure Price Quantity

Numeric Character Character Numeric Numeric

i. Add at least five records in item1<seatno>. Fields are separated by “ : ”.

ii. Sort them according to their prices.

iii. Sort the records according to their names

Solution:-

i.

[user37@centosserver ~]$ cat > item11

item code:item name:Unit of measure:price:Quatity

1:computer:kg:20000:5

16

Page 17: Roll Linux)

Roll Number:-33

2:TV:kps:30000:4

3:Milk:liter:40000:3

4:Suger:kg:50000:1

5:potatochips:gm:10:6

ii.

[user15@centosserver ~]$ sort -t" " +3 -4 item11

1:computer:kg:20000:5

2:TV:kps:30000:4

3:Milk:liter:40000:3

4:Suger:kg:50000:1

iii.

[user15@centosserver ~]$ sort -t" " +1 -2 item11

1:computer:kg:20000:5

3:Milk:liter:40000:3

4:Suger:kg:50000:1

2:TV:kps:30000:4

d)

1. To create a file named fod1 with some contents having the following contents and

display it in (i) octal form only and (ii) octal form along with its text contents.

2. To create two files named fcmp1 and fcmp2 which consists of at least five lines

with two or three

similar lines. Construct the commands using cmp

(i) to check whether the files differ

(ii) to compare the two files byte by byte.

3. Using comm. : - (i)to compare the files fcmp1 and fcmp2 (ii)to display the lines

which are

unique to fcmp1 and fcmp2 (iii)to display the lines which are common to the fcmp1

and fcmp2.

17

Page 18: Roll Linux)

Roll Number:-33

4. using diff : -(i) to display the lines which are common to both , the lines which are

not

common to both (ii) to display the difference in context output format (iii) to display

the unified

output format.

5. Create a file funiq1 with atleast 5 lines. Using uniq

(i) to remove the duplicate lines in funiq1.

(ii) to count the duplications and prepend number to each line

(iii) to display the duplicate lines only

Solution:-

1.i.

[user15@centosserver ~]$ cat > fod1

my name is rahedegs

sdgdfhh fsgd hffgfh

dssfhg fdhjhjj

[user15@centosserver ~]$ od -b fod1

0000000 155 171 040 156 141 155 145 040 151 163 040 162 141 150 145 144

0000020 145 147 163 012 163 144 147 144 146 150 150 040 146 163 147 144

0000040 040 150 146 146 147 146 150 012 144 163 163 146 150 147 040 146

0000060 144 150 152 150 152 152 012

0000067

ii.

[user15@centosserver ~]$ od -b -c fod1

0000000 155 171 040 156 141 155 145 040 151 163 040 162 141 150 145 144

m y n a m e i s r a h e d

0000020 145 147 163 012 163 144 147 144 146 150 150 040 146 163 147 144

e g s \n s d g d f h h f s g d

0000040 040 150 146 146 147 146 150 012 144 163 163 146 150 147 040 146

h f f g f h \n d s s f h g f

0000060 144 150 152 150 152 152 012

18

Page 19: Roll Linux)

Roll Number:-33

d h j h j j \n

0000067

2.

[user15@centosserver ~]$ cat > fcmp1

my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

[user15@centosserver ~]$ cat > fcmp2

my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

i.

[user15@centosserver ~]$ diff fcmp1 fcmp2

1c1

< my name is priyanka

---

> my name is priyanka

ii.

[user15@centosserver ~]$ cmp -l fcmp1 fcmp2

17 150 152

18 162 10

19 151 150

20 12 162

22 40 12

23 141 151

24 155 40

25 40 141

26 151 155

19

Page 20: Roll Linux)

Roll Number:-33

27 156 40

28 40 151

29 124 156

30 56 40

31 131 124

32 12 56

33 151 131

34 40 12

35 141 151

36 155 40

37 40 141

38 151 155

39 156 40

40 40 151

41 154 156

42 141 40

43 163 154

44 164 141

45 40 163

46 171 164

47 145 40

48 141 171

49 162 145

50 40 141

51 143 162

52 157 40

53 155 143

54 160 157

55 165 155

56 164 160

57 145 165

58 162 164

59 40 145

60 163 162

20

Page 21: Roll Linux)

Roll Number:-33

61 143 40

62 151 163

63 145 143

64 156 151

65 143 145

66 145 156

67 12 143

68 167 145

69 145 12

70 154 167

71 143 145

72 157 154

73 155 143

74 145 157

75 40 155

76 164 145

77 157 40

78 40 164

79 164 157

80 150 40

81 145 164

82 40 150

83 167 145

84 157 40

85 162 167

86 154 157

87 144 162

88 40 154

89 157 144

90 146 40

91 40 157

92 154 146

93 151 40

94 156 154

21

Page 22: Roll Linux)

Roll Number:-33

95 165 151

96 170 156

97 12 165

3.i.

[user15@centosserver ~]$ cmp fcmp1 fcmp2

fcmp1 fcmp2 differ: byte 17, line 1

ii.

[user15@centosserver ~]$ comm -3 fcmp1 fcmp2

my name is priyanka

i am in T.Y

i am in last year computer science

my name is priyanka

i am in T.Y

i am in last year computer science

iii.

my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

22

Page 23: Roll Linux)

Roll Number:-33

4.i.

[user15@centosserver ~]$ diff fcmp1 fcmp2

1c1

< my name is priyanka

---

> my name is priyanka

ii.

[user15@centosserver ~]$ diff -c fcmp1 fcmp2

*** fcmp1 2011-11-30 15:18:43.000000000 +0530

--- fcmp2 2011-11-30 15:20:38.000000000 +0530

***************

*** 1,4 ****

! my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

--- 1,4 ----

! my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

iii.

[user15@centosserver ~]$ diff -u fcmp1 fcmp2

--- fcmp1 2011-11-30 15:18:43.000000000 +0530

+++ fcmp2 2011-11-30 15:20:38.000000000 +0530

@@ -1,4 +1,4 @@

-my name is priyanka

+my name is priyanka

i am in T.Y

i am in last year computer science

welcome to the world of linux

23

Page 24: Roll Linux)

Roll Number:-33

5.

[user15@centosserver ~]$ cat > funiq1

rajeshri

rajeshri

rajendra

komal

sunita

sunita

i.

[user15@centosserver ~]$ uniq funiq1

rajeshri

rajendra

komal

sunita

ii.

[user15@centosserver ~]$ uniq -c funiq1

2 rajeshri

1 rajendra

1 komal

2 sunita

iii.

[user15@centosserver ~]$ uniq -d raj

rajeshri

sunita

24

Page 25: Roll Linux)

Roll Number:-33

Part II :

a) Create the file with the name gre1 and the following contents:

This is a first line.

This is a second line.

Please type the third line.

Do you wish to continue?

Simply type the fourth line.

b) Create the file with the name gre2 and the following contents:

The grep is an acronym for ‘globally search a regular expression and print it’. The

command searches the specified input globally for a match with the specified pattern

and displays it. While forming the pattern to be searched we can use shell

metacharacters, or regular expressions as professional unix users call them.

c) Do the following:

1. Search for the word ‘line’ and display the lines containing it.

2. Search for the word ‘the or ‘The’ in both the files gre1 and gre2 and display

the lines containing it.

3. Search for 4 letter words in gre1 and gre2 whose first character is ‘r’ and last

character is ‘r’ .

4. Display the lines, which end with the characters from s to z from gre1 and

gre2.

Solution:-

a)

[user15@centosserver ~]$ cat > gre1

This is a first line.

This is a second line.

Please type the third line.

Do you wish to continue?

Simply type the fourth line.

25

Page 26: Roll Linux)

Roll Number:-33

b)

[user15@centosserver ~]$ cat > gre2

The grep is an acronym for globally search a regular expression and print it. Th

e command searches the specified input globally for a match with the specified p

attern and displays it. While forming the pattern to be searched we can use shel

l metacharacters, or regular expressions as professional unix users call them.

c)

1.

[user15@centosserver ~]$ grep -i 'line' gre1

This is a first line.

This is a second line.

Please type the third line.

Simply type the fourth line.

2.

[user15@centosserver ~]$ grep -n the The gre1 gre2

grep: The: No such file or directory

gre1:3:Please type the third line.

gre1:5:Simply type the fourth line.

gre2:1:The grep is an acronym for globally search a regular expression and print

it. The command searches the specified input globally for a match with the spec

ified pattern and displays it. While forming the pattern to be searched we can u

se shell metacharacters, or regular expressions as professional unix users call

them.

3.

[user15@centosserver ~]$ grep r…..r gre1 gre2

4.

[user15@centosserver ~]$ grep [s-z]$ gre1 gre2

The command searches the specified input globally for a match with the specified

pattern and displays it.

26

Page 27: Roll Linux)

Roll Number:-33

d) Create a file empdata<seatno>, which contains following fields.

Fieldname Datatype Value

Employee last name character

Employee first name character

Employee code numeric Starts with letter ‘E’

Permanent address character

Department code character MKT, HRD, PUR

Grade character A-C

Years of experience numeric

Date of birth dd-mm-yy

Basic pay numeric

Insert at least five records in above file.

Character fields in each record may not be in the same case. '~' is used as a field

separator. Give commands to

1. Display all employees who are not in department MKT. Display the output

sorted on department code and grade.

2. Display all employees whose years of service are more than 5.

3. Store employee name and date of birth in a file bdata<seatno>.

4. Count total number of employees whose department code is HRD.

Solution:-

Employeelastname~Employeefirstname~Employeecode~PermantAddress~Deptcode~

Grade~YearofExperience~Dateofbirth~Basicpay

Gawde~Trupti~E101~mahim~HOD~A~2~23-10-1990~25000

Pate~Manali~E104~Dadar~HRD~B~6~11-11-1990~35000

Patil~Pramila~E105~Andheri~C~7~03-06-1990~40000

kajrolkar~rajeshri~E102~worli~MKT~A~2~2-10-1996~20000

Patil~Priyanka~E103~LoarParel~MKT~A~1~05-07-1991~30000

27

Page 28: Roll Linux)

Roll Number:-33

1.

[user15@centosserver ~]$ grep -v 'MKT' empdata15

Gawde~Trupti~E101~mahim~HOD~A~2~23-10-1990~25000

Pate~Manali~E104~Dadar~HRD~B~6~11-11-1990~35000

Patil~Pramila~E105~Andheri~C~7~03-06-1990~40000

[user15@centosserver ~]$ grep -v MKT empdata15|sort -t "~" -k 3

Gawde~Trupti~E101~mahim~HOD~A~2~23-10-1990~25000

kajrolkar~rajeshri~E102~worli~MKT~A~2~2-10-1996~20000

Patil~Priyanka~E103~LoarParel~MKT~A~1~05-07-1991~30000

Pate~Manali~E104~Dadar~HRD~B~6~11-11-1990~35000

Patil~Pramila~E105~Andheri~C~7~03-06-1990~40000

2.

[user15@centosserver ~]$ grep '~[5-9]~' empdata15

Pate~Manali~E104~Dadar~HRD~B~6~11-11-1990~35000

Patil~Pramila~E105~Andheri~C~7~03-06-1990~40000

3.

[user15@centosserver ~]$ cut -d "~" -f 1,2,8 empdata15 > bdata15

[user15@centosserver ~]$ cat bdata15

Employeelastname~Employeefirstname~Dateofbirth

kajrolkar~rajeshri~2-10-1996

Gawde~Trupti~23-10-1990

Patil~Priyanka~05-07-1991

Pate~Manali~11-11-1990

Patil~Pramila~40000

4.

[user15@centosserver ~]$ grep -c "HRD" empdata15

1

28

Page 29: Roll Linux)

Roll Number:-33

e) Create a file student<seatno> with following fields

Field Name Datatype Values

Student code character

Student name character

Batch code character Q11 - Q15

No. of modules Numeric 1 - 5

Average marks Numeric

Fields 7are separated by ":" (colon). Insert at least five appropriate records and

give the commands to

1. Display the details of student in order of their name ignoring case.

2. Display the details of student whose number of modules is greater than 3.

3. Store the list of first 5 rank holders in merit<seatno> file.

4. Count number of students in Batch Q13.

Solution:-

[user15@centosserver ~]$ cat > student15

studentcode:studentname:batchcode:No.ofmodules:Averagemarks

S101:Trupti:Q11:1:70

S102:Raj:Q12:2:75

S103:Priyanka:Q13:3:80

S104:Manali:Q14:4:85

S105:Pramila:Q15:5:90

1.

[user15@centosserver ~]$ sort -f -t ":"+1 - 2 student15

S104:Manali:Q14:4:85

S105:Pramila:Q15:5:90

S103:Priyanka:Q13:3:80

S102:Raj:Q12:2:75

S101:Trupti:Q11:1:7

29

Page 30: Roll Linux)

Roll Number:-33

2.

[user15@centosserver ~]$ grep ':[3-5]:' student15

S103:Priyanka:Q13:3:80

S104:Manali:Q14:4:85

S105:Pramila:Q15:5:90

3.

[user15@centosserver ~]$ sort -t":" -r +4 -5 student15 > merit15

S105:Pramila:Q15:5:90

S104:Manali:Q14:4:85

S103:Priyanka:Q13:3:80

S102:Raj:Q12:2:75

S101:Trupti:Q11:1:70

4.

[user15@centosserver ~]$ grep -c 'Q13' student15

2

30

Page 31: Roll Linux)

Roll Number:-33

Practical 3

Part I : vi editor

a) Create a file with the name fvi1 with atleast ten lines.

1. Perform the vi commands to the following cursor movements:

-3 words to the right

-4 words to the left

-Bottom of screen

-Beginning of current line

-Beginning of the file

-End of current line

-End of the file

Solution:-

[user04@centosserver ~]$ cat fvil

Internet has changed life. It has added new dimension to our existence by placin

g within easy reach and a mind. This has benefited to our day-to-day life. One o

f main benefit of its implementation is information communication which tells us

about the particular person and association rum by him.

SALUNKHE JEWELLERS established in the year 2008. Jewellery con

sumption in Salunkhe Jewellers has always been the bastion of old & renowned

jewellery house selling traditional gold, silver & diamonds. Manufactured using high-

grade raw material, our products are available in various sizes shapes,andesigns.

1.

-3 words to the right: 3w

-4 words to the left: 4b

-Bottom of screen: L

-Beginning of current line: 0

-Beginning of the file: H

-End of current line: $

-End of the file: G

b) Perform the vi commands to do the following deletions:

31

Page 32: Roll Linux)

Roll Number:-33

-character at the cursor

-one word from the current position

-current line

-next 4 lines

-line to the left of cursor

-line to the right of cursor

Solution:-

-character at the cursor x

-one word from the current position dw

-current line dd

-next 4 lines 4dd

-line to the left of cursor d0

-line to the right of cursor d$

Part II : ex-line editor

Create a file with the name fex1 having the contents as follows:

Shell Programming:

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

You can automate a lot of tasks with it.

The shell is very good for system administration tasks.

It is very useful for simple prototyping.

It is also useful for utilities that perform some relatively simple tasks where efficiency

is less important than ease of configuration, maintenance and portability.

Do the following:

1. Invoke this file from ex mode.

2. Give line numbers to the lines of included in the file.

3. Delete the last line.

4. Copy the first line after the last line.

5. Copy lines 2 to 5 after the last line.

32

Page 33: Roll Linux)

Roll Number:-33

6. Delete the line 1 to 5.

Solution:-

1.

[user37@centosserver ~]$ cat > fex1

"fex1" [New] 6L, 503C written

[user40@centosserver ~]$ ex fex1

"fex1" 6L, 503C

Entering Ex mode. Type "visual" to go to Normal mode

2.

:1,5#

1 Even though there are various graphical interfaces available for Linux the shell still

is a very neat tool. The shell is not just a collection of comma nds but a really good

programming language.

2 You can automate a lot of tasks with it.

3 The shell is very good for system administration tasks.

4 It is very useful for simple prototyping.

5 It is also useful for utilities that perform some relatively simple task s where

efficiency is less important than ease of configuration, maintenance and portability.

3.

:d

It is very useful for simple prototyping.

4.

:1 co $

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but

a really good programming language.

:1,$p

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

33

Page 34: Roll Linux)

Roll Number:-33

You can automate a lot of tasks with it.

The shell is very good for system administration tasks.

It is very useful for simple prototyping.

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

5.

:2,5 co $

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

:1,$p

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

You can automate a lot of tasks with it.

The shell is very good for system administration tasks.

It is very useful for simple prototyping.

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

You can automate a lot of tasks with it.

The shell is very good for system administration tasks.

It is very useful for simple prototyping.

Even though there are various graphical interfaces available for Linux the shell still is

a very neat tool. The shell is not just a collection of commands but a really good

programming language.

6.

:1,5 d

You can automate a lot of tasks with it.

34

Page 35: Roll Linux)

Roll Number:-33

Part II : sed editor

Create a file with five records with the name fsed1

and do the following using sed command:

1. Display first three lines.

2. Display the last line.

3. Display the third and fourth line.

4. Insert two more records and save the new file as newsed.

5. Delete the last two records from the file newsed.

Solution:-

[user37@centosserver ~]$ cat > fsed37

Priyanka patil

Snehal desai

Arati chavan

Chaitali keluskar

Swarda tate

1.

[user37@centosserver ~]$ sed '3q' fsed37

Priyanka patil

Snehal desai

Arati chavan

2.

[user37@centosserver ~]$ sed -n '$p' fsed37

Swarda tate

3.

[user37@centosserver ~]$ sed -n '3,4p' fsed37

Arati chavan

Chaitali keluskar

4.

35

Page 36: Roll Linux)

Roll Number:-33

[user37@centosserver ~]$ sed '$a

\kavita naik

\neha tharwal\' fsed37 > $$

[user37@centosserver ~]$ cat $$ > fsed1

[user37@centosserver ~]$ cat fsed1

Priyanka patil

Snehal desai

Arati chavan

Chaitali keluskar

Swarda tate

kavita naik

\neha tharwal

5.

[user37@centosserver ~]$ sed '$d' fsed1

Priyanka patil

Snehal desai

Arati chavan

Chaitali keluskar

Swarda tate

36

Page 37: Roll Linux)

Roll Number:-33

Practical 4

Basic Shell Scripting

WASP

1. To find the sum and product of integers.

Solution:-

$ vi Program

echo "Enter first number"

read num1

echo "Enter second number"

read num2

echo sum of $num1 and $num2 is $(expr $num1 + $num2)

echo product of $num1 and num2 is $(expr $num1 \* $num2)

Output:-

[user15@centosserver ~]$ $ bash Program

Enter first number

5

Enter second number

4

sum of 5 and 4 is 9

product of 5 and num2 is 20

2. To check whether the number is positive or negative using if …

with test and without test and bc utility.

Solution:-

$ vi num

echo "Enter no"

read a

if [ $a -lt 0 ]

then

echo "no. is positive"

elif [ $a -gt 0 ]

37

Page 38: Roll Linux)

Roll Number:-33

then

echo "no. is positive"

else

echo "no. is zero"

fi

Output:-

[user15@centosserver ~]$ $ bash num

Enter no

-5

no. is negative

3. To read the basic salary and find the gross salary.

Solution:-

$ vi sal

echo "Enter Basic salary"

read basic

DA=$(expr \( 20 \* $basic \) / 100)

HRA=$(expr \( 30 \* $basic \) / 100)

Tax=$(expr \( 10 \* $basic \) / 100)

Grass=$(expr $basic + $DA + $HRA - $Tax)

echo Gross salary is "$Grass"

Output:-

[user15@centosserver ~]$ $ bash sal

Enter Basic salary

1000

Gross salary is 1400

4. To check whether the file with the name entered exists or not.

Solution:-

$ vi fname

echo "Enter filename"

read fn1

if [ -e $fn1 ]

38

Page 39: Roll Linux)

Roll Number:-33

then

echo "file exists"

else

echo "file does not exist"

fi

Output:-

[user15@centosserver ~]$ $ bash sal

Enter filename

str

file exists

5. To compare the two strings.

Solution:-

$ vi str

echo "Enter 1st string"

read str1

echo "Enter 2nd string"

read str2

if [ $str1 = $str2 ]

then

echo "strings are equal"

else

echo "strings are not equal"

fi

Output:-

[user15@centosserver ~]$ $ bash sal

Enter 1st string

priyanka

Enter 2nd string

sonali

strings are not equal

6. To check whether the file has a permission to write.

39

Page 40: Roll Linux)

Roll Number:-33

Solution:-

$ vi fn

echo "Enter filename"

read f2

if [ -w $f2 ]

then

echo "File is writable"

else

echo "File is not writable"

fi

Output:-

[user15@centosserver ~]$ $ bash fn

Enter filename

file1

File is writable

7. To give grades using multiple if’s.

Solution:-

$ vi marks

echo "Enter student marks"

read m

if [ $m -ge 75 ]

then

echo "Grade A"

elif [ $m -ge 60 ]

then

echo "Grade B"

elif [ $m -ge 45 ]

then

echo "Grade C"

elif [ $m -ge 35 ]

then

40

Page 41: Roll Linux)

Roll Number:-33

echo "Grade D"

else

echo "Failed"

fi

Output:-

[user15@centosserver ~]$ $ bash marks

Enter student marks

60

Grade B

8. To check whether the number is +ve or –ve using if …elif.

Solution:-

$ vi num

echo "Enter the no"

read a

if [ $a -lt 0 ]

then

echo "No is negative"

elif [ $a -gt 0 ]

then

echo "No is positive"

else

echo "No is zero"

fi

Output:-

[user15@centosserver ~]$ $ bash num

Enter the no

1

No is positive

9. To print the day of the week using case…in

Solution:-

41

Page 42: Roll Linux)

Roll Number:-33

$ vi day

echo "Enter a day"

read n

case $n in

1) echo"Monday" ;;

2) echo "Tuesday" ;;

3) echo "Wednesday" ;;

4) echo "Thursday" ;;

5) echo "Friday" ;;

6) echo "Saturday" ;;

7) echo "Sunday" ;;

*) echo "no day" ;;

esac

Output:-

[user15@centosserver ~]$ $ bash day

Enter a day

4

Thursday

10. To find the type of the character entered.

Solution:-

$ vi char

echo "Enter Character"

read n

case $n in

[a-z]) echo "Small" ;;

[A-Z]) echo "Upper" ;;

[0-9]) echo "Number" ;;

*) echo "Special Character" ;;

esac

Output:-

42

Page 43: Roll Linux)

Roll Number:-33

[user15@centosserver ~]$ $ bash char

Enter Character

-

Special Character

11. To find the pattern of the string entered using case….in.

Solution:-

$ vi case

echo "Enter string"

read n

case $n in

[aeiou]*) echo "String begin with small letter of vowel";;

[AEIOU]*) echo "String begin with upper letter of vowel";;

[0-9]*) echo "String start with digit" ;;

*[0-9]) echo "String end with digit" ;;

?????) echo "String contain 5 character";;

*) echo "Invalid";;

esac

Output:-

[user15@centosserver ~]$ $ bash day

Enter string

aparna

String begin with small letter of vowel

12. To find the type of the file.

Solution:-

$ vi file

echo "Enter the file name"

read a

if [ -f $a ]

then

echo "file exist"

43

Page 44: Roll Linux)

Roll Number:-33

elif [ -c $a ]

then

echo "file is character"

elif [ -d $a ]

then

echo "file is directory"

elif [ -b $a ]

then

echo "block special"

else

echo "file does not exists"

fi

Output:-

[user15@centosserver ~]$ $ bash file

Enter the file name

file1

file exist

13. To prepare a menu driven program.

Solution:-

$ vi menu

echo "Press 1 to see the present working directory"

echo "press 2 to create a new file"

echo "press 3 to see the content of directory"

echo "press 4 to sort the file"

echo "press 5 to see all user currently login"

echo "Enter a menu no"

read n

case $n in

1) \pwd;;

2) echo "enter a filename"

read f

cat > $f;;

44

Page 45: Roll Linux)

Roll Number:-33

3)\ls;;

4) echo "Enter a filename"

read f1

\sort $f1;;

5)who;;

esac

Output :-

[user40@centosserver ~]$ $ bash file

echo "Press 1 to see the present working directory"

echo "press 2 to create a new file"

echo "press 3 to see the content of directory"

echo "press 4 to sort the file"

echo "press 5 to see all user currently login"

echo "Enter a menu no"

1

/home/user40

[user40@centosserver ~]$ bash file

Press1 To see the current working directory

Press2 create a new file with some contents

Press3 to see the contents of the directory

Press4 to sort a file

Press5 to see all the user currently log in

Enter your choice

2

enter the filename

shweta22

bhavana

[user40@centosserver ~]$ bash file

Press1 To see the current working directory

Press2 create a new file with some contents

Press3 to see the contents of the directory

Press4 to sort a file

Press5 to see all the user currently log in

45

Page 46: Roll Linux)

Roll Number:-33

Enter your choice

3

4090 emp236 funiq1 mbox pract5_1 shweta22 xaf xan x

a2 fcmp1 fvi1 merit236 pract5_2 s_test xag xao

abc236 fcmp2 fvi2 p2689 pract5_3 students236 xah xap

assig fex1 gre1 prac5_10 pract5_4 xaa xai xaq

bdata236 fod1 gre2 prac5_11 pract5_5 xab xaj xar

bhav236 fsed1 h_test prac5_12 pract5_6 xac xak xas

bhavana fsed2 item1_236 prac5_8 pract5_7 xad xal xat

bhavana236 fsp_236 item2_236 prac5_9 shewta xae xam xau

[user40@centosserver ~]$ bash file

Press1 To see the current working directory

Press2 create a new file with some contents

Press3 to see the contents of the directory

Press4 to sort a file

Press5 to see all the user currently log in

Enter your choice

4

Enter a file name

shweta22

bhavana

[user40@centosserver ~]$ bash file

Press1 To see the current working directory

Press2 create a new file with some contents

Press3 to see the contents of the directory

Press4 to sort a file

Press5 to see all the user currently log in

Enter your choice

5

root :0 2010-10-05 13:06

user27 pts/1 2010-10-05 13:14 (192.168.11.100)

akshaya pts/2 2010-10-05 13:20 (192.168.11.30)

user28 pts/3 2010-10-05 13:20 (192.168.11.97)

user17 pts/4 2010-10-05 13:22 (192.168.11.208)

46

Page 47: Roll Linux)

Roll Number:-33

user2 pts/7 2010-10-05 13:23 (192.168.11.64)

user23 pts/8 2010-10-05 13:23 (192.168.11.210)

user41 pts/11 2010-10-05 13:23 (192.168.11.239)

user13 pts/10 2010-10-05 13:23 (192.168.11.238)

user5 pts/13 2010-10-05 13:23 (192.168.11.234)

user40 pts/14 2010-10-05 13:24 (192.168.11.114)

user18 pts/16 2010-10-05 13:24 (192.168.11.237)

user14 pts/15 2010-10-05 13:24 (192.168.11.23)

user24 pts/19 2010-10-05 13:25 (192.168.11.220)

user8 pts/17 2010-10-05 13:25 (192.168.11.232)

user15 pts/21 2010-10-05 13:26 (192.168.11.211)

user7 pts/18 2010-10-05 13:27 (192.168.11.240)

user6 pts/12 2010-10-05 13:27 (192.168.11.236)

user12 pts/20 2010-10-05 13:31 (192.168.11.102)

user26 pts/5 2010-10-05 13:38 (192.168.11.209)

user44 pts/22 2010-10-05 14:48 (192.168.11.110)

user20 pts/9 2010-10-05 14:55 (192.168.11.158)

14. To print first n numbers and their sum using while loop to print.

Solution:-

$ vi num1

echo "Enter No"

read n

num=1

sum=0

while [ $num -le $n ]

do

sum=$(expr $sum + $num)

num=$(expr $num + 1)

done

echo The sum is $sum

47

Page 48: Roll Linux)

Roll Number:-33

Output:-

[user40@centosserver ~]$ bash num1

Enter No

5

The sum is 15

15. To call another file.

Solution:-

$ vi call

echo Press1 to compare the string

echo Press2 to check whether the no is +ve or -ve.

echo Press3 to obtain the sum and product

echo "enter ur choice"

read n

case $n in

1)bash str

;;

2)bash num

;;

3)bash Program

;;

*)echo Invalid

;;

Esac

Output:-

[user15@centosserver ~]$ $ bash call

echo Press1 to compare the string

echo Press2 to check whether the no is +ve or -ve.

echo Press3 to obtain the sum and product

echo "enter ur choice"

1

Enter 1st string

priyanka

48

Page 49: Roll Linux)

Roll Number:-33

Enter 2nd string

arati

strings are not equal

[user15@centosserver ~]$ $ bash call

echo Press1 to compare the string

echo Press2 to check whether the no is +ve or -ve.

echo Press3 to obtain the sum and product

echo "enter ur choice"

2

Enter the no

7

No is positive

[user15@centosserver ~]$ $ bash call

echo Press1 to compare the string

echo Press2 to check whether the no is +ve or -ve.

echo Press3 to obtain the sum and product

echo "enter ur choice"

3

Enter first number

5

Enter second number

4

sum of 5 and 4 is 9

product of 5 and num2 is 20

16. To print first n numbers and their sum using do…until.

Solution:-

$ vi sum

echo "Enter no"

read n

num=1

sum=0

until [ $num -gt $n ]

do

49

Page 50: Roll Linux)

Roll Number:-33

sum=$(expr $sum + $num)

num=$(expr $num + 1)

done

echo The sum is $sum

Output:-

[user15@centosserver ~]$ $ bash sum

Enter no

6

The sum is 21

17. To demonstrates the use of for loop.

Solution:-

$ vi sum

sum=0

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

do

sum=$( expr $sum + $i )

done

echo sum=$sum

Output :-

[user15@centosserver ~]$ $ bash sum

sum=55

18. To display the directories and files.

Solution:-

\ls;;

Output:-

4090 emp236 funiq1 mbox pract5_1 shweta22 xaf xan x

a2 fcmp1 fvi1 merit236 pract5_2 s_test xag xao

abc236 fcmp2 fvi2 p2689 pract5_3 students236 xah xap

assig fex1 gre1 prac5_10 pract5_4 xaa xai xaq

bdata236 fod1 gre2 prac5_11 pract5_5 xab xaj xar

50

Page 51: Roll Linux)

Roll Number:-33

bhav236 fsed1 h_test prac5_12 pract5_6 xac xak xas

bhavana fsed2 item1_236 prac5_8 pract5_7 xad xal xat

bhavana236 fsp_236 item2_236 prac5_9 shewta xae xam xau

51

Page 52: Roll Linux)

Roll Number:-33

Practical 5

Advanced Shell Scripting

Ex.1

(A)Write a shell script to generate following series 1,3,2,4,3,5,4,6,--- 100

Write down the shell script in the answer sheet. Execute it and show it to the

examiner.

(B) Give a command to display following results:

1) The day is : (mm/dd/yy)

2) The weekday is : (name of weekday)

(Replace the brackets with the actual values)

Write down the commands in the answer sheet. Execute them and show them to the

examiner.

(A)

Solution:-

$ vi s

i=1

echo $i

while [ $i -lt 10 ]

do

i=`expr $i + 2`

echo $i

i=`expr $i - 1`

echo $i

done

Output:-

[user15@centosserver ~]$ $ bash s

1

3

2

52

Page 53: Roll Linux)

Roll Number:-33

4

3

5

4

6

5

7

6

8

7

9

8

10

9

11

10

(B)

Solution:-

$ vi s1

set `date`

case $2 in

Jan)m=01

;;

Feb)m=02

;;

Mar)m=03

;;

Apr)m=04

;;

May)m=05

;;

Jun)m=06

;;

53

Page 54: Roll Linux)

Roll Number:-33

July)m=07

;;

Aug)m=08

;;

Sep)m=09

;;

Oct)m=10

;;

Nov)m=11

;;

Dec)m=12

;;

*)echo Invalid Month

;;

esac

echo the day is $m/$3/$6

echo the week day is $1

Output:-

[user40@centosserver ~]$ bash s1

the day is 10/13/2010

the week day is Wed

Ex.2

(A)

i) Give correct commands for following

1. Display your current working directory.

2. List all files starting with letter ‘C’.

3. Display your terminal address.

4. Create a subdirectory COMP<seatno>. Change to this directory & create

two files comp1<seatno> and comp2<seatno>.

5. Make a copy of file comp2<seatno> into file comp3<seatno>. Display

their inode numbers.

54

Page 55: Roll Linux)

Roll Number:-33

Write down the commands in the answer sheet. Execute them and show them to

the examiner.

ii) Write a shell script, which accepts an input from user and check-entered number

is positive, negative or zero.

Write down shell script in the answer sheet. Execute it and show it the

examiner.

(B)Create a shell script to print summation of following series

1! + 2! + 3! + ----- + N!

Write down shell script in the answer sheet. Execute it and show it the examiner.

(A) i)

Solution:-

$ vi prac6_3

echo Press1 Display the current working directory

echo Press2 List all files starting with letter C

echo Press3 Display a terminal address

echo Press4 Create a sub directory Comp240 Change to this directory & create 2 f

iles Comp1240 & Comp2240

echo Press5 Make a copy of file Form1233 into Form2233 display this inode

number

s

echo Enter your choice

read c

case $c in

1)\pwd

;;

2)\ls c*

;;

3)\tty

;;

4)\mkdir Comp240

\cd Comp240

\touch Comp1240 Comp2240

55

Page 56: Roll Linux)

Roll Number:-33

;;

5)\cp Form1233 Form2233

\ls -i Form1233 Form2233

;;

*)echo Invalid No.

esac

Output:-

[user40@centosserver ~]$ bash prac6_3

Press1 Display the current working directory

Press2 List all files starting with letter C

Press3 Display a terminal address

Press4 Create a sub directory Comp240 Change to this directory

prac6_3: line 4: create: command not found

prac6_3: line 4: Comp2240: command not found

Press5 Make a copy of file Form1233 into Form2233 display this inode numbers

Enter your choice

1

/home/user40

[user40@centosserver ~]$ bash prac6_3

Press1 Display the current working directory

Press2 List all files starting with letter C

Press3 Display a terminal address

Press4 Create a sub directory Comp240 Change to this directory

prac6_3: line 4: create: command not found

prac6_3: line 4: Comp2240: command not found

Press5 Make a copy of file Form1233 into Form2233 display this inode numbers

Enter your choice

2

ls: c*: No such file or directory

[user40@centosserver ~]$ bash prac6_3

Press1 Display the current working directory

Press2 List all files starting with letter C

56

Page 57: Roll Linux)

Roll Number:-33

Press3 Display a terminal address

Press4 Create a sub directory Comp240 Change to this directory

prac6_3: line 4: create: command not found

prac6_3: line 4: Comp2240: command not found

Press5 Make a copy of file Form1233 into Form2233 display this inode numbers

Enter your choice

3

/dev/pts/17

[user40@centosserver ~]$ bash prac6_3

Press1 Display the current working directory

Press2 List all files starting with letter C

Press3 Display a terminal address

Press4 Create a sub directory Comp240 Change to this directory

prac6_3: line 4: create: command not found

prac6_3: line 4: Comp2240: command not found

Press5 Make a copy of file Form1233 into Form2233 display this inode numbers

Enter your choice

4

mkdir: cannot create directory `Comp240': File exists

[user40@centosserver ~]$ bash prac6_3

Press1 Display the current working directory

Press2 List all files starting with letter C

Press3 Display a terminal address

Press4 Create a sub directory Comp240 Change to this directory

prac6_3: line 4: create: command not found

prac6_3: line 4: Comp2240: command not found

Press5 Make a copy of file Form1233 into Form2233 display this inode numbers

Enter your choice

5

cp: cannot stat `Form1233': No such file or directory

ls: Form1233: No such file or directory

427655 Form2233

57

Page 58: Roll Linux)

Roll Number:-33

ii)

Solution:-

$ vi shell7

echo "Enter the number:"

read a

if [ $a -gt 0 ]

then

echo "positive no."

elif [ $a -eq 0 ]

then

echo "the no. is zero"

else

echo "negative no."

fi

Output:-

[user23@centosserver ~]$ bash shell7

Enter the number:

5

positive no.

(B)

Solution:-

$ vi res

echo enter no

read n

i=1 fact=1 sum=0

while [ $i -le $n ]

do

fact=`expr $i \* $fact`

sum=`expr $sum + $fact`

i=`expr $i + 1`

done

58

Page 59: Roll Linux)

Roll Number:-33

echo fact=$fact

echo sum=$sum

Output:-

[user40@centosserver ~]$ bash res

enter no=

5

fact=120

sum=153

Ex.3

(A)Write a shell script, which displays number of ordinary files, directories and

special files in the current working directory. Write down the shell script on the

answer sheet. Execute it and show it to the examiner.

(B) Point out the errors in the following commands

a. find -print -type d

b. find 2001 print name j*

Write down the errors in the answer sheet.

(A)

Solution:-

$ vi r

c=0

for i in *

do

if [ -d $i ]

then

c=`expr $c + 1`

fi

done

echo directory=$c

59

Page 60: Roll Linux)

Roll Number:-33

j=0

for j in *

do

if [ -f $j ]

then

e=`expr $e + 1`

fi

done

echo ordinary=$e

k=0

for k in *

do

if [ -c $k ]

then

k=`expr $k + 1`

fi

done

echo special=$k

Output:-

[user40@centosserver ~]$ bash r

directory=1

ordinary=100

special=xav

(B)

Solution:-

a)

[user40@centosserver ~]$ find -print -type d.

./xaj

./product236

./pract5_4

./gre1

60

Page 61: Roll Linux)

Roll Number:-33

./prac6_10

./prime.out

./prac5_15

./prac5_8

./prac5_12

./fod1

./prac6_6

./b-233

./prac5_9

./mbox

./bhavana

b)

[user40@centosserver ~]$ find 2001 print name j*

find: 2001: No such file or directory

find: print: No such file or directory

find: name: No such file or directory

find:j*:Nosuchfileordirectory

61

Page 62: Roll Linux)

Roll Number:-33

Ex.4

(A)Write a shell script to create a file, which stores the names of the files and against

each name, put either 'morning', 'evening' or 'afternoon' depending upon the time

when file was created. Write a shell script in answer sheet, execute it and show it

to the examiner.

(B) Give a command to display the following

a. The name of your current working directory is __________

b. The name of your HOME directory is ___________

Write down the commands in the answer sheet, execute them and show them to the

examiner.

(A)

Solution:-

$ vi prac6_4

echo Enter the Filename

read f

hrs=`ls -l | grep $f | cut -d " " -f 9 | cut -c 1,2`

if [ $hrs -le 12 -a $hrs -gt 0 ]

then

echo Morning

elif [ $hrs -le 16 -a $hrs -gt 12 ]

then

echo Afternoon

elif [ $hrs -le 19 -a $hrs -gt 16 ]

then

echo Evening

else

echo Night

fi

Output:-

[user40@centosserver ~]$ bash prac6_4

Enter the Filename

prac6_1

62

Page 63: Roll Linux)

Roll Number:-33

Afternoon

(B)

Solution:-

a.

pwd

b.

echo $HOME

Ex.5

Create a file named student<seatno> with fields (Rollno, Student name, Marks).

Fields are separated by colon (:).

Write a shell script to perform the following options

1. Print the content of the file.

2. Delete the given file.

3. Rename the file.

4. If above three are not then print error message.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

Using the above file student<seatno> perform the following

1. Display students whose name starts with ‘A’.

2. Display students in the ascending order of marks.

Write down the commands in the answer sheet, execute them and show them to the

examiner.

Solution:-

cat > stud24

stdroll:studentname:marks

101:Trupti:1:70

102:ankita:75

103:Priyanka:80

S104:Manali:85

S105:Pramila:90

63

Page 64: Roll Linux)

Roll Number:-33

$ vi prac6_5

echo Press1 To print the content of file

echo Press2 Delete the given file

echo Press3 Rename the file

echo Press4 file does not exist

echo Enter your choice

read c

case $c in

echo Press1 To print the content of file

echo Press2 Delete the given file

echo Press3 Rename the file

echo Press4 file does not exist

echo Enter your choice

read c

case $c in

1) cat stud24

;;

2)rm stud24

;;

3)mv –i stud24 stud25

;;

4)

echo Enter file name

read a

If[ ! -f stud24 ]

echo file does not exist

fi

;;

*)

echo Invalid choice

esac

64

Page 65: Roll Linux)

Roll Number:-33

Output:-

[user40@centosserver ~]$ bash prac6_5

Press1 To print the content of file

Press2 Delete the given file

Press3 Rename the file

Press4 file does not exist

Enter your choice

1

101:Trupti:1:70

102:ankita:75

103:Priyanka:80

S104:Manali:85

S105:Pramila:90

[user40@centosserver ~]$ bash prac6_5

Press1 To print the content of file

Press2 Delete the given file

Press3 Rename the file

Press4 file does not exist

Enter your choice

2

[user40@centosserver ~]$ bash prac6_5

Press1 To print the content of file

Press2 Delete the given file

Press3 Rename the file

Press4 file does not exist

Enter your choice

3

Mv : overwrite ‘stud25’ ? y

$

[user40@centosserver ~]$ bash prac6_5

Press1 To print the content of file

Press2 Delete the given file

Press3 Rename the file

65

Page 66: Roll Linux)

Roll Number:-33

Press4 file does not exist

Enter your choice

4

Enter file name

stud

file does not exist

1.

Solution:-

[user40@centosserver ~]$ grep -e 'A*' std24

102:ankita:75

2.

Solution:-

[user40@centosserver ~]$ sort -n -t " " +2 -3 std24

101:Trupti:1:70

102:ankita:75

103:Priyanka:80

S104:Manali:85

S105:Pramila:90

Ex.6

(A)Write a shell script, which allows the user to add unique records in the file

product<seatno> (fields are product code, product name, price and quantity on

hand). Insert at least five records in the product<seatno> file.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

(B) Give the following commands based on above shell script

1. To display the contents of the product<seatno> file sorted on price.

2. Count total products listed in the product<seatno> file.

3. Display prices of products in ascending order along with product name.

66

Page 67: Roll Linux)

Roll Number:-33

Write down the commands in the answer sheet, execute them and show

them to the examiner.

(A)

Solution:-

$ vi prac6_6

echo Enter product id

read pid

echo Enter product name

read pname

echo Enter product price

read pc

echo Enter product quantity

read qty

str=`echo $pid : $pname : $pc : $qty`

l=`grep $pid product236 | wc -l`

if [ $l -gt 0 ]

then

echo duplicat values not allowed

else

echo $str >> product236

echo Record Inserted

fi

Output:-

[user40@centosserver ~]$ bash prac6_6

Enter product id

01

Enter product name

rice

Enter product price

40

Enter product quantity

1

67

Page 68: Roll Linux)

Roll Number:-33

Record Inserted

Cat product236

01 rice 40 1

02 oil 42 2

03 powder 35 3

04 soap 15 5

05 perfume 50 1

(B)

Solution:-

1.

[user40@centosserver ~]$ sort -n -t ":" +2 -3 product236

05 perfume 50 1

02 oil 42 2

01 rice 40 1

03 powder 35 3

04 soap 15 5

2.

[user40@centosserver ~]$ wc -l product236

5 product236

3.

[user40@centosserver ~]$ sort -n -t ":" +2 -3 product236

05 perfume 50 1

02 oil 42 2

01 rice 40 1

03 powder 35 3

04 soap 15 5

Ex.7

68

Page 69: Roll Linux)

Roll Number:-33

(A)Write a shell script that asks the user to input a number N and displays the squares

of all numbers from 1 to N as follows.

1 square = _____

2 square = _____

.

.

N square = _____

Write down the shell script in the answer sheet, execute it & show it to the

examiner.

(B)Create a file student<seatno> with following fields

Fieldname Datatype Value

Student code character

Student name character

Batch code character Q11-Q15

No. of modules Numeric 1-5

Fields are separated by ":" (colon). Insert at least five records in above file. Give

command to

1. Display the students belonging to batch codes Q12 to Q15.

2. Count total students in batch Q13.

Write down the commands in the answer sheet, execute them and show them to the

examiner.

(A)

Solution:-

$ vi prac6_7

echo Enter the number

read n

i=1

while [ $i -lt $n ]

do

str=`expr $i \* $i`

echo $i square is $str

i=`expr $i + 1`

done

69

Page 70: Roll Linux)

Roll Number:-33

Output:-

[user40@centosserver ~]$ bash prac6_7

Enter the number

5

1 square is 1

2 square is 4

3 square is 9

4 square is 16

(B)

Solution:-

[user40@centosserver ~]$ cat > ss233

SCode : SName : Batch : Modules :Marks

1:shweta:Q11:1:80

2:pooja:Q12:2:90

3:sagar:Q13:3:70

4:satish:Q14:4:50

5:reshma:Q15:5:60

1.

[user40@centosserver ~]$ grep -e "Q12" -e "Q13" -e "Q14" -e "Q15" ss233

2:pooja:Q12:2:90

3:sagar:Q13:3:70

4:satish:Q14:4:50

5:reshma:Q15:5:60

2.

[user40@centosserver ~]$ grep -c "Q13" ss233

1

Ex.8

70

Page 71: Roll Linux)

Roll Number:-33

(A)Generate a shell script, which accepts a user name. If user is not present then

appropriate message should be displayed otherwise tell the user to logout. Wait for 5

seconds and then kill the given user’s jobs.

[ NOTE : User will be provided by the examiner.]

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

Create a file named file<seatno> of your choice and try out the grep command to

search for the following pattern along with the following options.

1. To print only a count of line that contains a pattern.

2. Ignore uppercase/lowercase distinction during comparison.

3. Print all lines except those that contain a pattern.

Write down the commands in the answer sheet, execute them and show them to the

examiner.

Solution:-

$ vi prac6_8

echo Enter user name

read u

who > SHWETA

echo $u

m=`grep $u SHWETA | wc -l`

echo $m

if [ $m -gt 0 ]

then

echo User valid

else

echo Invalid user

fi

Output:-

Enter user name

user5

1

User valid

71

Page 72: Roll Linux)

Roll Number:-33

Cat > seatno5

SCode : SName : Batch : Modules :Marks

1:priyanka:Q11:2:80

2:pranita:Q12:1:70

3:sadhana:Q13:3:70

4:sneha:Q14:2:82

5:shraddha:Q15:5:60

Solution:-

1.

[user40@centosserver ~]$ grep 'sneha' seatno5 | wc -l

1

2.

[user40@centosserver ~]$ grep '[Tt]he' rew

The

3.

[user40@centosserver ~]$ grep [^shraddha] seatno5

1:priyanka:Q11:2:80

2:pranita:Q12:1:70

3:sadhana:Q13:3:70

4:sneha:Q14:2:82

Ex.9

72

Page 73: Roll Linux)

Roll Number:-33

(A) Generate a shell script that will check for every minute, how many users logged

in.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

Give the proper commands for the following

1. List all files starting either with letter ‘A’ or ‘C’.

2. Store the contents of ls –l output into a file called “list<seatno>” and the

output of who into “list1<seatno>”. Concatenate both the files into a

single display and store into another file called “final<seatno>”. Display

the content of file “final<seatno>”.

3. Display the login name of all users currently logged in the system.

Write down the commands in the answer sheet, execute them and show them to the

examiner.

(A)

Solution:-

$ vi prac6_9

echo Enter Time

read t

a=` who | cut -c 34,35,36,37,38 | grep -c $t`

echo No. of Users logged in at time $t are $a

Output:-

[user40@centosserver ~]$ bash prac6_9

Enter Time

14:58

No. of Users logged in at time 14:58 are 1

Solution:-

1.

[user40@centosserver ~]$ ls [AC]*

Comp1240 Comp2240

2.

73

Page 74: Roll Linux)

Roll Number:-33

[user40@centosserver ~]$ ls -l > list233

[user40@centosserver ~]$ cat list233

total 432

-rw-rw-r-- 1 user40 user40 98 Sep 7 13:51 4090

-rw-rw-r-- 1 user40 user40 151 Jan 11 18:05 a

-rw-rw-r-- 1 user40 user40 7 Jan 10 20:42 abc

-rw-rw-r-- 1 user40 user40 192 Jul 27 15:15 abc236

-rw-rw-r-- 1 user40 user40 229 Jan 11 18:38 as

-rw-rw-r-- 1 user40 user40 182 Jan 11 18:12 b

-rw-rw-r-- 1 user40 user40 107 Jan 8 19:42 b-233

-rw-rw-r-- 1 user40 user40 147 Aug 7 14:45 bdata236

-rw-rw-r-- 1 user40 user40 212 Jul 27 15:01 bhav236

-rw-rw-r-- 1 user40 user40 0 Oct 5 15:03 bhavana

-rw-rw-r-- 1 user40 user40 166 Jul 27 14:17 bhavana236

drwxrwxr-x 2 user40 user40 4096 Oct 13 16:34 Comp240

-rw-rw-r-- 1 user40 user40 159 Jan 11 18:45 d

-rw-rw-r-- 1 user40 user40 265 Jan 8 19:34 emp233

-rw-rw-r-- 1 user40 user40 316 Aug 7 14:45 emp236

-rw-rw-r-- 1 user40 user40 0 Aug 24 14:18 fcmp1

-rw-rw-r-- 1 user40 user40 101 Aug 14 13:58 fcmp2

[user40@centosserver ~]$ who > list1-233

[user40@centosserver ~]$ cat list1-233

root :0 2011-01-11 13:48

user40 pts/6 2011-01-11 17:58 (192.168.11.162)

[user40@centosserver ~]$ cat list233 list1-233 > final23

[user40@centosserver ~]$ cat final233

total 432

-rw-rw-r-- 1 user40 user40 98 Sep 7 13:51 4090

-rw-rw-r-- 1 user40 user40 151 Jan 11 18:05 a

-rw-rw-r-- 1 user40 user40 7 Jan 10 20:42 abc

-rw-rw-r-- 1 user40 user40 192 Jul 27 15:15 abc236

-rw-rw-r-- 1 user40 user40 229 Jan 11 18:38 as

-rw-rw-r-- 1 user40 user40 182 Jan 11 18:12 b

74

Page 75: Roll Linux)

Roll Number:-33

-rw-rw-r-- 1 user40 user40 107 Jan 8 19:42 b-233

-rw-rw-r-- 1 user40 user40 147 Aug 7 14:45 bdata236

-rw-rw-r-- 1 user40 user40 212 Jul 27 15:01 bhav236

-rw-rw-r-- 1 user40 user40 0 Oct 5 15:03 bhavana

-rw-rw-r-- 1 user40 user40 166 Jul 27 14:17 bhavana236

drwxrwxr-x 2 user40 user40 4096 Oct 13 16:34 Comp240

-rw-rw-r-- 1 user40 user40 159 Jan 11 18:45 d

-rw-rw-r-- 1 user40 user40 265 Jan 8 19:34 emp233

-rw-rw-r-- 1 user40 user40 316 Aug 7 14:45 emp236

-rw-rw-r-- 1 user40 user40 0 Aug 24 14:18 fcmp1

-rw-rw-r-- 1 user40 user40 101 Aug 14 13:58 fcmp2

root :0 2011-01-11 13:48

user40 pts/6 2011-01-11 17:58 (192.168.11.162)

.

3.

$ vi prac6_10

echo Enter user name

read u

who > shweta

echo $u

m=`grep $u shweta | wc -l`

echo $m

if [ $m -gt 0 ]

then

echo user valid

else

echo user invalid

fi

Output:-

[user40@centosserver ~]$ bash pract6_10

Enter user name

user24

75

Page 76: Roll Linux)

Roll Number:-33

1

user valid

Ex.10

(A) Write a shell script to add, multiply, divide and subtract any two numbers.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

(B) Create a file named name<seatno> with fields (firstname, lastname, salary) .

Insert at least five records in above file

Perform following sort operations

(i) Sort on first names only.

(ii) Sort on second names only.

(iii) Sort on salary only.

Write the command in the answer sheet, execute them and show them to the

examiner.

(A)

Solution:-

$ vi prac6_11

echo Enter the first no

read a

echo Enter the secoond no

read b

echo "sum=" `expr $a + $b`

echo "multi=" `expr $a \* $b`

echo "div=" `expr $a \/ $b`

echo "sub=" `expr $a \- $b`

Output:-

[user40@centosserver ~]$ bash pract6_11

Enter the first no=

8

76

Page 77: Roll Linux)

Roll Number:-33

Enter the secoond no=

4

sum= 12

multi= 32

div= 2

sub= 4

(B)

Cat >seatno6

FName:LName:Salary

pranita:rane:78000

aparna:nirgun:11000

sayali:bhalekar:22300

shweta:naik:22000

arati:chavan:15000

Solution:-

1.

[user40@centosserver ~]$ sort -n -t ":" +0 -1 t seatno6

aparna:nirgun:11000

arati:chavan:15000

pranita:rane:78000

sayali:bhalekar:22300

shweta:naik:22000

2.

[user40@centosserver ~]$ sort -n -t ":" +1 -2 seatno6

sayali:bhalekar:22300

arati:chavan:15000

shweta:naik:22000

aparna:nirgun:11000

pranita:rane:78000

77

Page 78: Roll Linux)

Roll Number:-33

3.

[user40@centosserver ~]$ sort -n -t ":" +1 -2 seatno6

pranita:rane:78000

sayali:bhalekar:22300

shweta:naik:22000

arati:chavan:15000

aparna:nirgun:11000

Ex.11

(A) Write a shell script which accept a single argument and indicate whether this

argument is digit, lowercase letter , uppercase letter or special character.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

(B) Write a shell script to generate prime number series up to N.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

(A)

Solution:-

$ vi prac6_12

echo enter letter

read l

case $l in

[a-z])echo small

;;

[A-Z])echo capital

;;

[0-9])echo numbers

;;

?)echo special characters

;;

*)echo invalid input

78

Page 79: Roll Linux)

Roll Number:-33

;;

esac

Output:-

[user40@centosserver ~]$ bash prac6_12

enter letter

A

capital

(B)

Solution:-

#include<stdio.h>

main(void)

{

int n,flag=0,i,j;

printf("enter the number n=");

scanf("%d",&n);

for(i=3;i<=n;i++)

{

flag=0;

for(j=2;j<=i-1;j++)

{

if(i%j==0)

{

flag=1;

}

}

if(flag==0)

{

printf("%d",i);

}

}

}

79

Page 80: Roll Linux)

Roll Number:-33

Output:-

[user41@centosserver ~]$ gcc -o primeno.out pract10.c

[user41@centosserver ~]$ ./primeno.out

enter the number n=7

3 5 7

Ex.12

(A)Write a shell script to find GCD & LCM of two positive numbers.

Write down the shell script in the answer sheet, execute it and show it to the

examiner.

(B) Write a shell script, which takes two arguments. The first argument is pattern &

second is filename. Check whether the pattern supplied is found in the file or not.

If found store it in other file named output<seatno> and display the content of

output file with line numbers.

Write down the shell script in the answer sheet, execute it and show to the

examiner.

(A)

Solution:-

$ vi prac6_12

echo "Enter value for a"

read a

echo "Enter value for b"

read b

if [ $a -lt $b ]

then

t=$a

a=$b

b=$a

r=` expr $a / $b`

fi

while [ $r -gt 0 ]

80

Page 81: Roll Linux)

Roll Number:-33

do

a=$b

b=$r

r=`expr $a % $b`

done

echo GCD=`expr $b`

echo LCM=`expr $a \* $b / $b`

Output:-

[user40@centosserver ~]$ bash prac6_12

Enter value for a

6

Enter value for b

9

GCD=3

LCM=18

(B)

Solution:-

$ vi output236

if [ $# -gt 2 -o $# -lt 2 ]

then

echo “Invalid no.arguments”

elif [ -f$S2 ]

then

echo `grep –n $1 $2` > output236

cat output236

else

echo Invalid filename

fi

Output:-

81

Page 82: Roll Linux)

Roll Number:-33

[user40@centosserver ~]$ cat output236

2:01:bhavana:q11:2:450

4:02:archana:q12:1:350

6:03:riya:q13:5:250

82