10MCA17 UNIX Programs [MCA SEM 2, VTU]

65
# Program 1A # Write a non-recursive shell script which accepts any number of arguments and prints # them in reverse order clear if test $# -eq 0 then echo "NO COMMAND ARGUMENTS PASSED" else echo "THE REVERSE IS:" c=$# while [ $c -ne 0 ] do eval echo -n "\$$c" echo -n " " c=` expr $c - 1 ` done echo "" fi

Transcript of 10MCA17 UNIX Programs [MCA SEM 2, VTU]

Page 1: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 1A

# Write a non-recursive shell script which accepts any number of arguments and prints

# them in reverse order

clear

if test $# -eq 0

then

echo "NO COMMAND ARGUMENTS PASSED"

else

echo "THE REVERSE IS:"

c=$#

while [ $c -ne 0 ]

do

eval echo -n "\$$c"

echo -n " "

c=` expr $c - 1 `

done

echo ""

fi

Page 2: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 1A.sh

NO COMMAND ARGUMENTS PASSED

2. $ sh 1A.sh a b c

THE REVERSE IS:

c b a

Page 3: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 1B

# Write a shell script that accepts two file names as arguments, checks if the

# permissions for these files are identical, if yes output the common permissions,

# otherwise output each file name followed by its permissions

clear

ls -l $1 | cut -d " " -f1 > f1

ls -l $2 | cut -d " " -f1 > f2

if cmp f1 f2

then

echo "BOTH FILES HAVE IDENTICAL PERMISSIONS"

cat f1 f2

else

echo "BOTH FILES DO NOT HAVE SAME PERMISSIONS"

echo "1st FILE PERMISSION: "

cat f1

echo "2nd FILE PERMISSION: "

cat f2

fi

Page 4: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 1B.sh a b

BOTH FILES HAVE IDENTICAL PERMISSIONS

-rwxr-xr-x

-rwxr-xr-x

2. $ sh 1B.sh a c

f1 f2 differ: byte 3, line 1

BOTH FILES DO NOT HAVE SAME PERMISSIONS

1st FILE PERMISSION:

-rwxr-xr-x

2nd FILE PERMISSION:

-r-xr-xr-x

Page 5: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 2A

# Write a shell script that takes a valid directory name as an argument and recursively

# descend all the subdirectories, finds the maximum length of any file in that hierarchy

# and writes this maximum value to the standard output

clear

if test $# -ne 1

then

echo "INVALID NUMBER OF ARGUMENTS, ENTER ONLY ONE ARGUMENT"

else

if test -e $1

then

if test -d $1

then

echo "REVERSE ORDER HEIRARCHY:"

ls -lR $1

echo ""

echo "FILE WITH THE HIGHEST SIZE:"

ls -lR $1 | grep ^- | tr -s " " | cut -d " " -f5 | sort -n | tail -1

else

echo "$1 IS NOT A DIRECTORY"

fi

else

echo "$1 DOESN'T EXISTS"

fi

fi

Page 6: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 2A.sh

INVALID NUMBER OF ARGUMENTS, ENTER ONLY ONE ARGUMENT

2. $ sh 2A.sh 1a

1a IS NOT A DIRECTORY

3. $ sh 2A.sh try

REVERSE ORDER HEIRARCHY:

try:

total 56

-rwxr-xr-x 1 ABHILASH ABHILASH 76 2011-12-12 15:44 add.sh

-rwxr-xr-x 1 ABHILASH ABHILASH 20020 2011-12-12 15:44 a.out

-rwxr-xr-x 1 ABHILASH ABHILASH 114 2011-12-12 15:44 inform.sh

-rwxr-xr-x 1 ABHILASH ABHILASH 343 2011-12-12 15:44 permissions.sh

-rwxr-xr-x 1 ABHILASH ABHILASH 154 2011-12-12 15:44 reverse.sh

FILE WITH THE HIGHEST SIZE:

20020

Page 7: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 2B

# Write a shell script that accepts a path name and creates all the components in that

# path name as directories

clear

if [ $# -le 0 -o $# -gt 1 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

mkdir -p $*

echo "ALL DIRECTORIES CREATED"

Page 8: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 2B.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 2B.sh x/y/z

ALL DIRECTORIES CREATED

$ tree x

x

`-- y

`-- z

2 directories, 0 files

# Program 3A

Page 9: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Write a shell script which accepts valid log-in names as arguments and prints their

# corresponding home directories, if no arguments are specified, print a suitable error

# message

clear

if [ $# -eq 0 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

fi

for i in "$@"

do

cat /etc/passwd | cut -d ":" -f1>user

grep -w "$i" user>/dev/null

if [ $? -eq 0 ]

then

echo "VALID LOGIN"

echo "THE HOME DIRECTORY IS: "

cat /etc/passwd | grep -w ^"$i" | cut -d ":" -f6

else

echo "INVALID LOGIN"

fi

done

Page 10: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 3A.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 3A.sh student

INVALID LOGIN

3. $ sh 3A.sh ABHILASH

VALID LOGIN

THE HOME DIRECTORY IS:

/home/ABHILASH

Page 11: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 3B

# Write a shell script to implement terminal locking. Is should prompt the user for a

# password. After accepting the password entered by the user, it must prompt again for

# the matching password as confirmation and if match occurs, it must lock the keyword

# until a matching password is entered again by the user. Note that the script must

# written to disregard BREAK, ctrl+d. no time limit need be implemented for the lock

# duration

clearstty echowhile truedoecho "ENTER THE PASSWORD"read pass1clearecho "RE-ENTER THE PASSWORD"read pass2clearif [ $pass1 = $pass2 ]thenecho "TERMINAL LOCKED"echo "TO UNLOCK ENTER THE PASSWORD"pass1=" "until [ "$pass1" = "$pass2" ]doecho "ENTER CORRECT PASSWORD"read pass1cleardoneclearecho "TERMINAL UNLOCKED"stty echoexitelseecho "PASSWORD MISMATCHED, RETYPE THEM"stty echofidone

Page 12: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 3B.sh

ENTER THE PASSWORD

abhi

RE-ENTER THE PASSWORD

abi

PASSWORD MISMATCHED, RETYPE THEM

ENTER THE PASSWORD

abhi

RE-ENTER THE PASSWORD

abhi

TERMINAL LOCKED

TO UNLOCK ENTER THE PASSWORD

ENTER CORRECT PASSWORD

abi

ENTER CORRECT PASSWORD

abhi

TERMINAL UNLOCKED

Page 13: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 4A

# Crete a script file called file-properties that’s reads a file name entered and outputs it

# properties

clear

echo "ENTER A FILE NAME"

read file

case $file in

"") echo "INVALID ENTRY";;

*) if [ -f $file ]

then

ls -l $file

else

echo "THE FILE $file IS NOT FOUND"

fi;;

esac

Page 14: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 4A.sh

ENTER A FILE NAME

1z

THE FILE 1z IS NOT FOUND

2. $ sh 4A.sh

ENTER A FILE NAME

1a

-rwxr-xr-x 1 ABHILASH ABHILASH 398 2011-12-13 16:21 1a

Page 15: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 4B

# Write a shell script that accepts one or more filenames as arguments & convert all of

# them to uppercase, provided they exist in current directory

clear

if [ $# -eq 0 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

for fn in "$@"

do

if [ -f $fn ]

then

echo $fn | tr '[a-z]' '[A-Z]'

else

echo "FILE NOT FOUND: $fn"

fi

done

Page 16: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 4B.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 4B.sh test13a

TEST13A

Page 17: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 5A

# Write a shell script that displays all the links to a file specified as the first argument to

# the script. The second argument, which is optional, can be used to specify in which the

# search is to begin. If the second argument is not present, the search is to begin in

# current working directory. In either case, the starting directory as well as all its

# subdirectories at all levels must be searched. The script need not include any error

# checking

clear

if [ $# -gt 0 -a $# -le 2 ]

then

if [ $# -eq 1 ]

then

ls -lR $1 | tr -s " " | cut -d " " -f2,8

else

cd $2

ls -lR $1 | tr -s " " | cut -d " " -f2,8

fi

else

echo "INVALID NUMBER OF ARGUMENTS"

fi

Page 18: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 5A.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 5A.sh 1a

1 1a

3. $ sh 5A.sh db.lst sample

1 db.lst

Page 19: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 5B

# Write a shell script that accept a file name as argument and display its creation time if

# file exists and if it doesn’t send output error message

clear

if [ $# -ne 1 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

if [ -e $1 ]

then

echo "THE FILE $1 WAS CREATED ON: `ls -l $1 | tr -s " " | cut -d " " -f6,7 ` "

else

echo "FILE $1 NOT FOUND"

fi

Page 20: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 5B.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 5B.sh 1z

FILE 1z NOT FOUND

3. $ sh 5B.sh 1a

THE FILE 1a WAS CREATED ON: 2011-12-13 16:21 1a

Page 21: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 6A

# Write a shell script to display the calendar for current month with current date

# replaced by * or ** depending on whether the date has one digit or two digits

clear

date=`date +%d`

if [ $date -ge 10 ]

then

cal | sed "s/$date/**/"

else

cal | sed "s/$date/*/"

fi

Page 22: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 6A.sh

January 2012

Su Mo Tu We Th Fr Sa

1 2 3 4 5 * 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

2. $ sh 6A.sh

January 2012

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7

8 9 10 ** 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

Page 23: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 6B

# Write a shell script to find smallest of three numbers that are read from keyboard

clear

echo "ENTER THREE NUMBERS"

read a b c

if [ $a -lt $b ]

then

if [ $a -lt $c ]

then

echo "$a IS THE SMALLEST NUMBER"

else

echo "$c IS THE SMALLEST NUMBER"

fi

else

if [ $b -lt $c ]

then

echo "$b IS THE SMALLEST NUMBER"

else

echo "$c IS THE SMALLEST NUMBER"

fi

fi

Page 24: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 6B.sh

ENTER THREE NUMBERS

12 65 23

12 IS THE SMALLEST NUMBER

2. $ sh 6B.sh

ENTER THREE NUMBERS

65 12 23

12 IS THE SMALLEST NUMBER

3. $ sh 6B.sh

ENTER THREE NUMBERS

65 23 12

12 IS THE SMALLEST NUMBER

Page 25: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 7A

# Write a shell script using expr command to read in a string and display a suitable

# message if it does not have at least 10 chracters

clear

echo "ENTER A STRING"

read str

len=`expr length $str`

if test $len -ge 10

then

echo "THE STRING CONTAINS $len CHARACTERS"

else

echo "THE STRING CONTIANS LESSER THAN TEN CHARACTERS"

fi

Page 26: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 7A.sh

ENTER A STRING

abhi

THE STRING CONTIANS LESSER THAN TEN CHARACTERS

2. $ sh 7A.sh

ENTER A STRING

abhilash.h.m.

THE STRING CONTAINS 13 CHARACTERS

Page 27: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 7B

# Write a shell script to compute the sum of numbers passed to it as arguments on

# command line & display the result

clear

if [ $# -eq 0 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

for fn in "$@"

do

sum=`expr $sum + $fn`

done

echo "THE SUM OF ALL NUMBERS: $sum"

Page 28: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 7B.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 7B.sh 1 2 3 4 5 6 7 8 9 10

THE SUM OF ALL NUMBERS: 55

Page 29: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 8A

# Write a shell script that compute gross salary of an employee, according to the rule:

# If basic salary is < 15000, HRA=10% of basic & DA=90% of basic

# If basic salary is >= 15000, HRA=500 of basic & DA=98% of basic

clear

echo "ENTER THE BASIC SALARY OF THE EMPLOYEE"

read sal

if [ $sal -lt 15000 ]

then

hra=`expr $sal \* 0.10`

da=`expr $sal \* 0.90`

gross=`expr $sal + $hra + $da`

echo

echo "BASIC SALARY = $sal"

echo "HRA = $hra"

echo "DA = $da"

echo "-------------------------"

echo "GROSS SALARY = $gross"

echo "-------------------------"

echo

else

hra=500

da=`expr $sal \* 0.98`

gross=`expr $sal + $hra + $da`

echo

echo "BASIC SALARY = $sal"

echo "HRA = $hra"

echo "DA = $da"

echo "-------------------------"

echo "GROSS SALARY = $gross"

echo "-------------------------"

echo

fi

Page 30: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 8A.sh

ENTER THE BASIC SALARY OF THE EMPLOYEE

10000

BASIC SALARY = 10000

HRA = 1000

DA = 9000

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

GROSS SALARY = 20000

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

2. $ sh 8A.sh

ENTER THE BASIC SALARY OF THE EMPLOYEE

15000

BASIC SALARY = 15000

HRA = 500

DA = 14700

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

GROSS SALARY = 30200

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

Page 31: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 8B

# Write a shell script that deletes all lines containing a specific word in one or more file

# supplied as argument to it

clear

if [ $# -lt 1 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

echo "ENTER THE WORD TO DELETE"

read word

for file in "$@"

do

grep -iv $word $file | tee 1> /dev/null $file

done

Page 32: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ cat >test

apple

ball

cat

dog

[ctrl+d]

2. $ sh 8B.sh

INVALID NUMBER OF ARGUMENTS

3. $ sh 8B.sh test

ENTER THE WORD TO DELETE

apple

4. $ cat test

ball

cat

dog

Page 33: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 9A

# Write a shell script that gets executed displays the message either “Good Morning” or

# “Good Afternoon” or “Good Evening” depending upon time at which the user logs in

clear

h=`who | head -1 | tr -s " " | cut -d " " -f4 | cut -d ":" -f1`

if [ $h -lt 12 ]

then

echo "GOOD MORNING"

else

if [ $h -ge 12 -a $h -lt 17 ]

then

echo "GOOD AFTERNOON"

else

echo "GOOD EVENING"

fi

fi

Page 34: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 9A.sh

GOOD MORNING

2. $ sh 9A.sh

GOOD AFTERNOON

3. $ sh 9A.sh

GOOD EVENING

Page 35: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 9B

# Write a shell script that accept a list of filenames as its arguments, count & report

# occurrence of each word that is present in the first argument file on other argument

# file

clear

if [ $# -ne 2 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

str=`cat $1 | tr '\n' ' '`

for a in $str

do

echo "WORD=$a, COUNT=`grep -c "$a" $2`"

done

Page 36: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ cat test9b

satish

chidu

name

apple

chandra

2. $ cat test9b1

satish

chidu

chidu

name

apple

satish

name

apple

chidu

3. $ sh 9B.sh

INVALID NUMBER OF ARGUMENTS

4. $ sh 9B.sh test9b

INVALID NUMBER OF ARGUMENTS

5. $ sh 9B.sh test9b test9b1

WORD=satish, COUNT=2

WORD=chidu, COUNT=3

WORD=name, COUNT=2

WORD=apple, COUNT=2

WORD=chandra, COUNT=0

Page 37: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 10A

# Write a shell script that determines the period for which a specified user is working

# on the system

clear

echo "ENTER USERNAME"

read user

tm=`who | grep -w "$user" | tr -s " " | head -1 | cut -d " " -f4`

l=`echo $tm | wc -c`

if [ $l -eq 1 ]

then

echo "INVALID USERNAME"

exit

fi

uhr=`echo $tm | cut -d ":" -f1`

umin=`echo $tm | cut -d ":" -f2`

shr=`date "+%H"`

smin=`date "+%M"`

m=`expr $smin - $umin`

h=`expr $shr - $uhr`

if [ $m -lt 0 ]

then

m=`expr $m + 60`

h=`expr $h - 1`

fi

echo "USERNAME: $user"

echo "LOGGED IN AT: $uhr HOURS & $umin MINUTES"

echo "LOGGED IN PERIOD: $h HOURS & $m MINUTES"

Page 38: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 10a.sh

ENTER USERNAME

student

INVALID USERNAME

2. $ sh 10a.sh

ENTER USERNAME

ABHILASH

USERNAME: ABHILASH

LOGGED IN AT: 19 HOURS & 50 MINUTES

LOGGED IN PERIOD: 0 HOURS & 1 MINUTES

Page 39: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 10B

# Write a shell script that reports the logging in of a specified user within one minute

# after he/she login. The script automatically terminates if specified user doesn’t log in

# during a specified period of time

clear

echo "ENTER USERNAME"

read user

tm=`who | grep -w "$user" | tr -s " " | head -1 | cut -d " " -f4`

l=`echo $tm | wc -c`

if [ $l -eq 1 ]

then

echo "INVAILD USERNAME"

exit

fi

uhr=`echo $tm | cut -d ":" -f1`

umin=`echo $tm | cut -d ":" -f2`

shr=`date "+%H"`

smin=`date "+%M"`

m=`expr $smin - $umin`

h=`expr $shr - $uhr`

if [ $m -lt 0 ]

then

m=`expr $m + 60`

h=`expr $h - 1`

fi

if [ $h -eq 0 -a $m -lt 1 ]

then

echo "$user HAS LOGGED IN JUST NOW"

else

echo "$user HAS LOGGED IN $h HOURS & $m MINUTES AGO"

fi

Page 40: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 10B.sh

ENTER USERNAME

student

INVAILD USERNAME

2. $ sh 10B.sh

ENTER USERNAME

ABHILASH

ABHILASH HAS LOGGED IN JUST NOW

3. $ sh 10B.sh

ENTER USERNAME

ABHILASH

ABHILASH HAS LOGGED IN 0 HOURS & 2 MINUTES AGO

Page 41: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 11A

# Write a shell script that accepts two integers as its argument and compute the value of

# first number raised to the power of second number

clear

if [ $# -ne 2 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

val=`echo "$2 ^ $1" | bc`

echo "$2 ^ $1 = $val"

Page 42: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 11A.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 11A.sh 2

INVALID NUMBER OF ARGUMENTS

3. $ sh 11A.sh 2 3

3 ^ 2 = 9

Page 43: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 11B

# Write a shell script that accept the file name, starting and ending line number as an

# argument to display all the lines between the given line number

clear

if [ $# -ne 3 ]

then

echo "INVLID NUMBER OF ARGUMENTS"

exit

fi

c=`expr $3 - $2 + 1`

cat $1 | head -$3 | tail -$c

Page 44: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ cat >file

-rw-rw-r-- 1 student student 254 2011-11-12 15:01 \

-rw-rw-r-- 1 student student 327 2011-11-12 10:34 1.l

-rw-rw-r-- 1 student student 493 2011-11-12 10:33 1.y

-rw-rw-r-- 1 student student 391 2011-11-12 11:42 2.l

-rw-rw-r-- 1 student student 0 2011-11-12 17:08 66lab.l

-rw-rw-r-- 1 student student 81 2011-11-12 17:37 6lab.l

-rw-rw-r-- 1 student student 205 2011-11-12 16:05 6lab.y

-rw-rw-r-- 1 student student 369 2011-09-30 14:19 7.l

-rw-rw-r-- 1 student student 6 2011-10-18 14:57 a1.c

-rw-rw-r-- 1 student student 7 2011-10-18 14:41 aa.c

-rw-rw-r-- 1 student student 736 2011-09-20 15:42 ad.l

-rw-rw-r-- 1 student student 1 2011-08-11 10:21 ad.sh

-rw-rw-r-- 1 student student 998 2011-11-12 10:30 alpha.l

-rw-rw-r-- 1 student student 345 2011-11-12 10:39 als.l

[ctrl+d]

2. $ sh 11B.sh

INVLID NUMBER OF ARGUMENTS

3. $ sh 11B.sh file 2 8

-rw-rw-r-- 1 student student 327 2011-11-12 10:34 1.l

-rw-rw-r-- 1 student student 493 2011-11-12 10:33 1.y

-rw-rw-r-- 1 student student 391 2011-11-12 11:42 2.l

-rw-rw-r-- 1 student student 0 2011-11-12 17:08 66lab.l

-rw-rw-r-- 1 student student 81 2011-11-12 17:37 6lab.l

-rw-rw-r-- 1 student student 205 2011-11-12 16:05 6lab.y

-rw-rw-r-- 1 student student 369 2011-09-30 14:19 7.l

Page 45: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 12A

# Write a shell script that folds long lines into 10 columns. Thus any lines that exceed 10

# characters must be broken after 10th, a “\” is to be appended as the indication of

# folding and the processing is to be continued with the residue. The input is to be

# supplied through a text file created by the user

{

len=length($0)

if ( len <= 10 )

print $0

else

{

for (i=1;i<len;i +=10)

{

print substr ($0,i,10)

}

print substr ($0,i)

}

}

Page 46: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ cat >test12abangalore is a beautiful city. it is full of greenarydelhi Indiadelhi is the capital city of India[ctrl+d]

2. $ awk -f 12A.sh test12abangalore is a beautiful city. it is full of greenary

delhiIndiadelhi is the capital city of India

Page 47: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 12B# Write a awk script that accepts date argument in the form of mm-dd-yy and display it# in the form if day, month & year. The script should check the validity of the argument# & in the case of error, display a suitable message

clear

if [ $# -ne 3 ]

then

echo "INVALID NUMBER OF ARGUMENTS"

exit

fi

echo "ENTER DATE IS:"

echo "$1-$2-$3"

if [ $1 -gt 12 -o $2 -gt 31 ]

then

echo "INVALID DATE! ENTER DATE IN MM DD YY FORMAT"

exit

fi

year=`expr $3 % 4`

if [ $1 -eq 2 -a $year -eq 0 -a $2 -gt 29 ]

then

echo "FEBRUARY CANNOT CONTAIN MORE THAN 29 DAYS!"

exit

fi

if [ $1 -eq 2 -a $year -ne 0 -a $2 -gt 28 ]

then

echo "FEBRUARY CANNOT CONTAIN MORE THAN 28 DAYS! $3 IS NOT A LEAP YEAR"

exit

fi

if [ $1 -eq 4 -o $1 -eq 6 -o $1 -eq 9 -o $1 -eq 11 ]

then

if [ $2 -eq 31 ]

then

echo "INVALID DAY"

exit

fi

fi

echo "$2-$1-$3"

Page 48: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ sh 12B.sh

INVALID NUMBER OF ARGUMENTS

2. $ sh 12B.sh 01 32 2011

ENTER DATE IS:

01-32-2011

INVALID DATE! ENTER DATE IN MM DD YY FORMAT

3. $ sh 12B.sh 13 30 2011

ENTER DATE IS:

13-30-2011

INVALID DATE! ENTER DATE IN MM DD YY FORMAT

4. $ sh 12B.sh 11 31 2011

ENTER DATE IS:

11-31-2011

INVALID DAY

5. $ sh 12B.sh 02 29 2011

ENTER DATE IS:

02-29-2011

FEBRUARY CANNOT CONTAIN MORE THAN 28 DAYS! 2011 IS NOT A LEAP

YEAR

6. $ sh 12B.sh 02 31 2012

ENTER DATE IS:

02-31-2012

FEBRUARY CANNOT CONTAIN MORE THAN 29 DAYS!

Page 49: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 13A

# Write an awk script to delete duplicated line from a text file. The order of the original

# lines must remain unchanged

BEGIN{

FS=" "

}

{

if(var!=$1 && NR>0)

print line

var=$1

line=$0

}

END{print line}

Page 50: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. $ cat >test13a

satish

chandra

chidu

chidu

fren

name

name

apple

apple

[ctrl+d]

2. $ awk -f 13A.sh test13a

satish

chandra

chidu

fren

name

apple

Page 51: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 13B

# Write an awk script to find out total number of books sold in each discipline as well as

# book sold using associate array down table as given below

# i. Electrical 34

# ii. Mechanical 67

# iii. Electrical 80

# iv. Computers 43

# v. Mechanical 65

# vi. Civil 198

# vii. Computers 64

BEGIN { printf "TOTAL NUMBER OF BOOKS SOLD IN EACH CATEGORY" }

{ books[$1]+=$2 }

END {for(item in books)

{ printf("\n%-17s%2s%-5d\n",item,"= ",books[item])

total+=books[item]

}

printf("\n%-17s%2s%-5d\n","TOTAL BOOKS SOLD","= ",total)

}

Page 52: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. cat >test13b

Electrical 34

Mechanical 67

Electrical 80

Computers 43

Mechanical 65

Civil 198

Computers 64

[ctrl+d]

2. $ awk -f 13B.sh test13b

TOTAL NUMBER OF BOOKS SOLD IN EACH CATEGORY

Civil = 198

Computers = 107

Mechanical = 132

Electrical = 114

TOTAL BOOKS SOLD = 551

Page 53: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# Program 14

# Write an awk script that compute gross salary of an employee, according to the rule:

# If basic salary is < 10000, HRA=15% of basic & DA=45% of basic

# If basic salary is >=15000, HRA=20% of basic & DA=50% of basic

BEGIN { FS = "\t"

print"\n\t\t\tSALARY STATEMENT OF EMPLOYEES\n"

print"-----------------------------------------------------------------------------------------"

print"SL.NO","\t\t","NAME","\t\t","DESIGNATION","\t","BASIC","\t\t","DA","\t","HRA","\

t","GROSS"

print"-----------------------------------------------------------------------------------------"}

{ slno++;basic_tot+=$4;

if($4>10000)

{ da=0.45*$4;da_tot+=da;

hra=0.15*$4; hra_tot+=hra;}

else { da=0.50*$4; da_tot+=da;

hra=0.20*$4;hra_tot+=hra;}

sal_tot+=$4+da+hra

printf"%2d\t\t%-20s%10-s%8d%15.2f%10.2f%10.2f\n",slno,$2,$3,$4,da,hra,$4+da+hra}

END {

print"\n-----------------------------------------------------------------------------------------\n"

print"TOTAL BASIC PAID:\t\tRs."basic_tot

print"TOTAL DA PAID:\t\t\tRs."da_tot

print"TOTAL HRA PAID:\t\t\tRs."hra_tot

print"TOTAL SALARY PAID:\t\tRs."sal_tot

print"" }

Page 54: 10MCA17 UNIX Programs [MCA SEM 2, VTU]

# OUTPUTS

1. cat >test14b

1 ABI DM 50000

2 PARU GM 56000

[ctrl+d]

2. $ awk -f 14B.sh test14b

SALARY STATEMENT OF EMPLOYEES

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

SL.NO NAME DESIGNATION BASIC DA HRA GROSS

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

1 ABI DM 50000 22500.00 7500.00 80000.00

2 PARU GM 56000 25200.00 8400.00 89600.00

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

TOTAL BASIC PAID: Rs.106000

TOTAL DA PAID: Rs.47700

TOTAL HRA PAID: Rs.15900

TOTAL SALARY PAID: Rs.169600