Anda di halaman 1dari 39

WEEK3:

1.
a) Login to the system
b) Use the appropriate command to determine your login shell
c) Use the /etc/passwd file to verify the result of step b
d) Use the who command and redirect the result to a file called myfile1. use the more
command to see the contents of myfile1.
e) Use the date and who commands 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 a file called
myfile2. use the more command to check the contents of myfile2.
f) Write a sed command that deletes the first characters in each line in a file.

b) determining the login shell


shell variable SHELL contains the path name for login shell
$ echo $SHELL
/bin/bash

c) searching the line of 13csea: from /etc/passwd


$ cat /etc/passwd : grep 13csea
13csea:!:259:209::/home/13csea:/usr/bin/ksh
$

d) redirecting output of who to myfile1


$ who > myfile1
$

More command to see the contents of myfile1 more myfile1 or cat


myfile1 | more
$ more myfile1
IT1 pts/1 May 20 16:13 (192.168.80.222)
it5 pts/2 May 20 16:13 (192.168.80.222)
myfile1: END

e) output of date command on screen and output of who to myfile2


$date; who > myfile2
Tue May 20 16:20:54 IST 2008

Contents of myfile2 by more command

$ more myfile2
13csea pts/0 May 20 16:18 (192.168.80.222)
12csea pts/1 May 20 16:13 (192.168.80.222)
13cseb pts/2 May 20 16:13 (192.168.80.222)
myfile2: END
2. a) Write sed command that deletes the first character in each line in a file
b) Write a sed command that deletes the character before the last character in each line in a
file.
c ) Write a sed command that swaps the first and second words in each line in a file.

a)deletes first character in each line in a file

$ cat temp
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita

echo Enter file name


read file
echo The contents of the file $file after deletion
sed 's/^.//' $file | tee tmp
mv tmp $file

Enter file name


temp
The contents of the file temp after deletion
425 15.65 Ravi
450 21.86 Raju
320 26.27 Ramu
830 36.15 Sita

b)deletes the character before the last character in each line in a file.

$ cat temp
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita

echo Enter file name


read file
echo The contents of file $file after deleting last but one character
sed "s/\(.*\).\(.$\)/\1\2/" $file | tee tmp
mv tmp $file
Enter file name
temp
The contents of file temp after deleting last but one character
1425 15.65 Rai
1450 21.86 Rau
4320 26.27 Rau
6830 36.15 Sia

c) swaps the first and second words in each line in a file.

$ cat names
Water:melon
Butter:milk

echo Enter file name


read file
echo The file after swaping first and second words in each line
sed 's/\(.*\):\(.*\)/\2:\1/' $file | tee tmp
mv tmp $file

Enter file name


names
The file after swaping first and second words in each line
melon:Water
milk:Butter

WEEK4:
a) Pipe your /etc/passwd file to awk, and print out the home directory of each user
b) Develop and interactive grep script that asks for a word and a file name and then tells
how many lines containing that word
c) Repeat
d) Part using awk

a) The password file has fields delimited by colons and home drectory is 6th field
$ awk < /etc/passwd -F: '{ print $6 }'

b) $cat data
welcome to nec
hai nec nec
butter milk
Shell script:
echo Enter a word
read word
echo Enter file name
read file
nol=$(grep -c $word $file)
echo $nol lines $word present in $file

output:
Enter a word
nec
Enter file name
data
2 lines nec present in data
c) & d)

Using awk for printing different fields of a data file employee

$ cat employee
1234|singh Chandan|CAO|34000
3456|srivastav susant|HOD|45000
7654|kumar ramesh|DIRECTOR|56000
2233|gunjan shilpa|PROGRAMMER|23000
5556|satpathi suresh|SYSTEM MANAGER|45600
$ awk -F"|" '{ print $2, $4}' < employee
singh Chandan 34000
srivastav susant 45000
kumar ramesh 56000
gunjan shilpa 23000
satpathi suresh 45600

WEEK5:
a) Write a shell script that takes a command line argument and reports on whether it is a
directory , a file, or something else.
b) Write a shell script that accepts one or more filenames as arguments and converts all of
them to uppercase, provided that they exist in the current directory.
c) Write a shell script that determines the period for which a specified user is working on
the system.
a) Shell script:
for file in $*
do
if [ -f $file ]
then
echo $file is a file
elif [ -d $file ]
then
echo $file is a directory
else
$file is neither directory nor file
fi
done

output: $sh 5a.sh employee


employee is a file
b) Shell script:
for file in $*
do
if [ .$file ]
then
echo $file belongs to current directory
a=`echo $file | tr "[a-z]" "[A-Z]" `
mv $file $a
echo $file is renames as $a
fi
done

output: $sh 5b.sh employee


employee belongs to current directory
employee is renamed as EMPLOYEE
c) echo Enter user name
read name
a=$(who| grep "$name" | cut -c25-38)
echo $name is logged in from $a
output:Enter user name
student
student is logged in from 15-03-22 19:27

WEEK6:
A) Write a shell script that accepts a filename starting and ending line numbers as arguments and
displays all the lines between the given line numbers
B) Write a shell script that deletes all lines containing specified word in one or more files
supplied as arguments to it.

a) Shell script:
echo Enter file name
read file
echo Enter starting line nunber
read start
echo Enter ending line number
read end
line=`expr $start + 1`
while [ $line -lt $end ]
do
head -$line $file | tail -1
line=`expr $line + 1`
done

output:$cat data
hai nec
hello nec
welcome to nec
Foss loab
programs
$sh 6a.sh
Enter file name
data
Enter starting line nunber
1
Enter ending line number
5
hello nec
welcome to nec
Foss loab

b) Shell script:
if [ $# -eq 0 ]
then
echo "please enter one more file name"
else
echo enter word to delete
read word
echo contents of file after deletion of $word
for file in $*
do
sed "/$word/d" $file |tee tmp
mv tmp $file
done
fi

output: $cat example


hai nec
hello nec
welcome to NEC
Nec
$sh 6b.sh example
enter word to delete
nec
contents of file after deletion of nec
welcome to NEC
Nec

WEEK7:
a) Write a shell script that computes the gross salary of a Employee according to folowing rules:
I) If basic salary is < 1500 the hra = 10% of the basic salary and da = 90% of the basic
II) If basic salary is >=1500 then hra=rs.500 and da=98% of the basic. The basic salary is entered
interactively through the keyboard.
b) write a shell script that accepts two integers as its arguments and computes the value of first
number raised to the power of second number.

a) Computing gross salary

echo Enter basic salary


read bs
if test $bs -lt 1500
then
HRA=`expr $bs \* 10 / 100 | bc`
DA=`expr $bs \* 90 / 100 | bc`
grosssalary=`expr $bs + $HRA + $DA |bc`
echo "Gross salary:$grosssalary"
else
HRA=500
DA=`expr $bs \* 98 / 100 | bc`
grosssalary=`expr $bs + $HRA + $DA |bc`
echo "Gross salary:$grosssalary"
fi

output:$sh 7a.sh
Enter basic salary
3000
Gross salary:6440

b) To compute number raised to power

echo "enter two numbers"


read num1
read num2
a=`echo $num1 ^ $num2 | bc`
echo $num1 raised to the power of $num2 is $a

output:$sh 7b.sh
enter two numbers
12
2
12 raised to the power of 2 is 144

WEEK8:
a) Write an interactive file-handling shell program. Let it offer the user the choice of
copying, removing, renaming or lining files. Once the user has made a choice, have the
program ask the user for the necessary information, such as the file name, new file name
and so on.
b) Write shell script that takes a login name as command line argument and reports when
person logs in
c) Write a shell script which receives two file names as arguments. It should check whether
the two file contents are same or not. If they are same then second file should be deleted.
a) Shell script:
echo 1.copy 2.Remove 3.Rename 4.Link
echo Enter your choice
read choice
case $choice in
1)echo Enter source file name
read source
echo Enter destination file name
read destination
cp $source $destination
echo $source is copied to $destination
;;
2)echo Enter file name you want to delete
read file
rm $file
$file is deleted
;;
3)echo Enter the file name you want to rename
read source
echo Enter new name
read new
mv $source $new
echo $source is renamed as $new
;;
4)echo Enter file name to link
read source
echo Enter anothe r file name
read target
var=$(pwd)
ln $source $var/$target
i1=$(ls -i $source|cut -c1-6)
i2=$(ls -i $var/$target|cut -c1-6)
if [ $i1 -eq $i2 ]
then
echo $source and $target are linked and have same inode number $i1
fi
;;
*)echo Invalid choice
;;
esac
output:$sh 8a.sh
1.copy 2.Remove 3.Rename 4.Link
Enter your choice
4
Enter file name to link
option.sh
Enter anothe r file name
eg.sh
option.sh and eg.sh are linked and have same
inode number 295344

b) Shell script
echo Enter name of user
read name
a=$(who | grep -i $name | cut -c23-38)
if [ $? -eq 0 ]
then
echo $name is logged at $a
fi

output:$sh 8b.sh
Enter name of user
student
student is logged at 2015-03-22 21:11
c) Shell script:
echo Enter file name
read f1
echo Enter another file name to compare
read f2
cmp $f1 $f2
exitstatus=$?
echo Exit status is $exitstatus
if [ $exitstatus -eq 0 ]
then
echo Two files are identical
rm $f2
echo $f2 is deleted
else
echo $f1 and $f2 are not identical
fi
output:$sh 8c.sh
Enter file name
cmp.sh
Enter another file name to compare
cmp1.sh
Exit status is 0
Two files are identical
cmp1.sh is deleted
WEEK 9:
a) Write a shell script that displays a list of all files in the current directory to which the user
has read, write and execute permissions
b) develop an interactive shell script that asks for a word ad filename and then tells how
many times that word occurred in a file.
c) Write a shell script to perform following string operations:
i) To extract a sub-string from a given string
ii) to find length of given string

a) Shell script:
echo Files which have read write and execute permissions
for file in `ls`
do
if [ -r $file a w $file a x $file]
then
echo $file
fi
done

output: $sh 9a.sh


Files which have read write and execute
permissions
mytable
data
example
b) Shell script:
echo "Enter a file name"
read file
echo Enter a word
read wrd
echo "wrd count is "
grep $wrd $file -o | wc l

output:$cat data
nec nec
hai nec
hello nec
welcome
$sh 9b.sh
Enter a file name
data
Enter a word
nec
wrd count is
4
c) Shell script:
echo Enter a string
read s1
length="${#s1}"
echo Length of sub string is $length
echo Enter the value of position from which you want to extract
read i
echo Enter the length of string to extract
read n
while [ $i -le $n ]
do
h=`expr $s1 | cut -c $i`
i=`expr $i + 1`
echo -n $h '\b'
done
echo "\n"
output: Enter a string
fosslabprograms
Length of sub string is 15
Enter the value of position from which you
want to extract
4
Enter the length of string to extract
5
slabp
WEEK 10:
Write a C program that takes one or more file or directory names as command line input and
reports the following information on the file:
a) file type
b) number of links
c) read, write and execute permission
d) Time of last access (use stat/fstat system calls)

#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct stat statbuf;
int k;
for(k=1;k<argc;k++)
{
if(lstat(argv[1], &statbuf)==1)
printf("can.t stat file\n");
else
{
printf("File : %s\n",argv[k]);
printf("Type and permission : %o\n",statbuf.st_mode);
printf("Number of links %d\n",statbuf.st_nlink);
printf("Time of last access: %s\n",ctime(&statbuf.st_atime));
}
}
}
The execution and output:
$ cc 10.c
$ ./a.out 8a 8b
File : 8a.sh
Type and permission : 100744
Number of links 1
Time of last access: Fri May 23 16:45:45 2008

File : 8b.sh
Type and permission : 100744
Number of links 1
Time of last access: Fri May 23 16:45:45 2008

Week11:
Write C programs that simulate the following UNIX command
a) mv b)cp (use system calls)

a)mv command:
#include <stdio.h>
#include<unistd.h> #include<fcntl.h>
main(int argc,char *argv[])
{
char buff[50];
int r,fd1,fd2;
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_CREAT|O_EXCL|O_WRONLY,0733);
for(;;)
{
r=read(fd1,buff,50);
if(r==0) break;
write(fd2,buff,r);
}
remove(fd1);
close(fd2);
}

The execution and output:


$cc 11a.c
$./a.out
Enter old filename : abc
Enter new filename: xyz
File abc renamed to xyz
$
a)cp command:
#include<stdio.h>
#include<unistd.h> #include<fcntl.h>
main(int argc,char*argv[])
{
char buff[50];
int r,fd1,fd2;
fd1=open(argv[1],O_RDONLY);
fd2=open(argv[2],O_CREAT|O_EXCL|O_WRONLY,0733);
for(;;)
{
r=read(fd1,buff,50);
if(r==0) break;
write(fd2,buff,r);
}
Close(fd1);
Close(fd2);
}

The execution and output:


$cc 11b.c
$./a.out example example1
Copy is successful.n

WEEK 12:
Write a C program that simulate ls command.
ls command:
#include <dirent.h>
#include <stdio.h>
int main(int argc, char **argv)
{
DIR *dir;
struct dirent *direntry;
if((dir=opendir(argv[1]))==NULL)
puts("Directory not found..");
while((direntry=readdir(dir))!=NULL)
printf("%10d%s\n", direntry->d_ino,direntry->d_name);
closedir(dir);
return (0);
}
The execution and output:

$ cc 12.c
$ ./a.out btech2
28675 .
16390 ..
28676 cut1
28677 cut2
28690 cut3
28739 emp
28740 hello
28741 myfile.txt
28742 myfile1
28743 myfile2
28744 mytable
28745 mytable1
28746 names
28747 t
28748 table
28749 temp
28750 week4b
28751 week5a
$ ./a.out btech1
Directory not found
$

WEEK 13: Do the following shell programs also

1)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.

Shell script:
a=$(who | grep -i $1 )
if [ $? -eq 1 ]
then
echo He is not logged in
exit
else
v=whoami
if [ $1 = $v ]
then
echo $1 is logged in but $1 cannot receive message
else
echo $1 is logged in $1 can receive message
fi
fi

output:$whoami
user
$sh 13.1.sh student
student is logged in but student cannot receive
message

2)write a shell script to accept the name of the file from standard input and perform the following
tests on it
a)File executable b)File readable c)File writable
d)Both readable and writable

Shell script:
if [ -x $1 ]
then
echo "The file has executable permission"
else
echo "The file has no executable permission"
fi
if [ -r $1 ]
then
echo "The file has readable permission"
r=1
else
echo "The file has no redable permission"
r=0
fi
if [ -w $1 ]
then
echo "The file has writable permissinon"
w=1
else
echo "The file has no writable permission"
w=0
fi
if [ r eq 1 a w eq 1 ]
then
echo "The file has both readable and weritable permission"
elif [ r eq 0 a w eq 1 ]
then
echo "The file has no readable but have writable permission"
elif [ r eq 1 ]
then
echo The file has readable but no writable permission
else
echo The file has no readable and writable permisssions
fi

output:$sh 13.2.sh example


The file has executable permission
The file has readable permission
The file has no writable permission
The file has readable and no writable
permissions

3)write a shell script which will display the username and terminal name who login recently into
the unix system

Shell script:
echo current user is
whoami
echo Terminal name is
tty

output:$sh 13.3.sh
current user is
student
Terminal name is
/dev/pts/0

4) write a shell script to find no. of files in a directory

Shell script:
count=0
for f in `ls`
do
count=`echo $count + 1 | bc`
done
echo No of files in current directory is $count
output:$sh 13.4.sh
No of files in current directory is 50

5)write a shell script to check whether a given number is perfect or not

Shell script:
echo "Enter a number :"
read num
i=1
count=0
while [ $i -lt $num ]
do
d=`expr $num % $i`
if [ $d -eq 0 ]
then
count=`expr $count + $i`
fi
i=`expr $i + 1`
done
if test $count -eq $num
then
echo " $num is a perfect number"
else
echo " $num is not a perfect number"
fi

output:$sh 13.5.sh
Enter a number :
28
28 is a perfect number
6)write a menu driven shell script to copy,edit,rename and delete a file

Shell script:
echo "1.Copy 2.Rename 3.Delete"
echo Enter your choice
read choice
case $choice in
1) cp $1 $2
echo $1 is copied to $2
;;
2)mv $1 $2
echo $1 is renamed as $2
;;
3)rm $1
echo $1 is deleted
;;
*)echo Ivalid choice
;;
esac
output: $sh 13.6.sh vowel.data
1.Copy 2.Rename 3.Delete
Enter your choice
2
vowel.data is renamed as vow.data
7)write a shell script for concatenation of two strings

Shell script:
echo "enter twos strings"
read s1
read s2
echo concatenated string is $s1$s2
output: $sh 13.7.sh
enter two strings
butter
milk
concatenated string is buttermilk

8)write a shell script which will display the Fibonacci series up to a given number of argument

Shell script:
echo "Enter length of Fibonacci series"
read length
a=0
b=1
i=0
echo Fibonacci series
while [ $i -lt $length ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
output: $sh 13.8.sh
Enter length of Fibonacci series
5
Fibonacci series
1
2
3
5
8

9) write a shell script to accept 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 A
Avg<80 && Avg>=70 then grade B
Avg<70 && Avg>=60 then grade C
Avg<60 && Avg >=50 then grade D
Avg<50 && Avg>=40 then grade E
Else grade F
Shell script:
echo "Enter student number"
read number
echo "Enter student name"
read name
echo "Maximum marks for each subject:100"
echo "Enter subject marks"
echo "First subject:"
read s1
echo "Second subject:"
read s2
echo "Third subject:"
read s3
echo "Fourth subject:"
read s4
echo "Fifth subject:"
read s5
total=`expr $s1 + $s2 + $s3 + $s4 + $s5`
echo "Total=$total"
average=`echo $total / 5 | bc`
echo "Average=$average"
if [ $s1 -lt 40 -o $s2 -lt 40 -o $s3 -lt 40 -o $s4 -lt 40 -o $s5 -lt
40 ]
then
echo "Fail"
exit
elif [ $average -ge 80 ]
then
echo "Grade=A"
elif [ $average -lt 80 -a $average -ge 70 ]
then
echo "Grade=B"
elif [ $average -lt 70 -a $average -ge 60 ]
then
echo "Grade=C"
elif [ $average -lt 60 -a $average -ge 50 ]
then
echo "Grade=D"
elif [ $average -lt 50 -a $average -ge 40 ]
then
echo "Grade=E"
else
echo "Grade=F"
fi
output: $sh 13.9.sh
Enter student number
12345
Enter student name
xxxxx
Maximum marks for each subject:100
Enter subject marks
First subject:
12
Second subject:
56
Third subject:
78
Fourth subject:
87
Fifth subject:
69
Total=302
Average=60
Fail
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, GROSSSAL and NETSAL. Also store
all details in a file called emp.dat
Rules:HRA is 18% of basic if basic>1500 otherwise 550
DA is 35% of basic
PF is 13% of basic
IT is 14% of basic
TA is 10% of basic

echo "salary structure"


echo "Enter employee number"
read empno
echo "Enter Employee name"
read empname
echo "Enter basic salary"
read basic
if [ $basic -gt 5000 ]
then
HRA=`echo $basic \* 18 / 100 | bc`
else
HRA=550
fi
DA=`echo $basic \* 35 / 100 | bc`
PF=`echo $basic \* 13 /100 | bc`
IT=`echo $basic \* 14 / 100 | bc`
TA=`echo $basic \* 10 / 100 | bc`
GROSSSAL=`echo $basic + $HRA + $TA + $DA | bc`
NETSAL=`echo $GROSSSAL - $IT - $PF | bc`
echo "Employee name:$empname Employee number:$empno"
echo "Basic=$basic"
echo "DA=$DA"
echo "PF=$PF"
o "salary structure"
echo "Enter employee number"
read empno
echo "Enter Employee name"
read empname
echo "Enter basic salary"
read basic
if [ $basic -gt 5000 ]
then
HRA=`echo $basic \* 18 / 100 | bc`
else
HRA=550
fi
DA=`echo $basic \* 35 / 100 | bc`
PF=`echo $basic \* 13 /100 | bc`
IT=`echo $basic \* 14 / 100 | bc`
TA=`echo $basic \* 10 / 100 | bc`
GROSSSAL=`echo $basic + $HRA + $TA + $DA | bc`
NETSAL=`echo $GROSSSAL - $IT - $PF | bc`
echo "Employee name:$empname Employee number:$empno"
echo "Basic=$basic"
echo "DA=$DA"
echo "PF=$PF"
echo "IT=$IT"
echo "TA=$TA"
echo "GROSS SALARY=$GROSSSAL"
echo "NET SALARY=$NETSAL"
output: $sh 13.10.sh
Enter employee number
5345
Enter Employee name
gdug
Enter basic salary
6000
Employee name:gdug
Employee number:5345
Basic=6000
DA=2100
PF=780
IT=840
TA=600
GROSS SALARY=9780
NET SALARY=8160
11)write a shell script to demonstrate break and continue statements
Shell script:
echo CONTINUE EXAMPLE
for i in 1 2 3 4 5
do
if [ $i -eq 3 ]
then
continue
else
echo $i
fi
done
echo BREAK EXAMPLE
for i in 1 2 3 4 5
do
if [ $i -eq 3 ]
then
break
else
echo $i
fi
done
output: $sh 13.11.sh
CONTINUE EXAMPLE
1
2
4
5
BREAK EXAMPLE
1
2
12)Write a shell script to satisfy the following menu options
a.Display current directory path
b.Display todays date
c.Display users who are connected to the unix system
d.Quit
Shell script:
echo "1.path 2.Date 3.Acounts 4.Quit"
echo "Enter choice"
read value
case $value in
1)
echo "Display current directory path"
pwd
var=$pwd
echo $var
;;
2)
echo "Displays today date"
date
v=$date
echo $v
;;
3)
echo "Accounts"
who
v1=$who
echo $v1
;;
4)
echo "QUIT"
exit
;;
*)
echo "Invalid choice"
;;
esac
output: $sh 13.12.sh
1.path 2.Date 3.Acounts 4.Quit
Enter choice
1
Display current directory path
/home/student/lab
1.path 2.Date 3.Acounts 4.Quit
Enter choice
2
Displays today date
Sat Mar 21 19:50:42 IST 2015
1.path 2.Date 3.Acounts 4.Quit
Enter choice
3
Accounts
student pts/0 2015-03-21 18:21 (:0)
1.path 2.Date 3.Acounts 4.Quit
Enter choice
4
QUIT
13)write a shell script to delete all files whose size is zero bytes from current directory
Shell script:
echo "Delets all files whose size is zero"
for i in `ls`
do
if [ .$i ]
then
if [ ! -s $i ]
then
echo "The file is $i"
rm $i
fi
fi
done
output: $sh 13.13.sh
Delets all files whose size is zero
The file is 0.sh
The file is rr
14)write a shell script to display string palindrome from given arguments
Shell script:
for word in $*
do
p=`echo $word | rev`
if [ $word = $p ]
then
echo $word is palindrome
else
echo $word is not palindrome
fi
done
output: $sh 13.14.sh madam exam
madam is palindrome
exam is not palindrome
15)write a shell script which will display Armstrong numbers from given arguments
Shell script:
for number in $*
do
r=0
a=$number
while [ $number -ne 0 ]
do
p=`echo $number % 10 | bc`
r=`echo $r + $p ^ 3 | bc`
number=`echo $number / 10 | bc`
done
if [ $a -eq $r ]
then
echo $a is armstrong number
else
echo $a is not an armstrong number
fi
done
output: $sh 13.15.sh 153 321
153 is an armstrong number
321 is not an armstrong number
16)write a shell script to display reverse numbers from given argument list
Shell script:
for word in $*
do
sum=0
n=$word
while [ $n -gt 0 ]
do
rem=`expr $n % 10`
sum=`expr $sum \* 10 + $rem`
n=`expr $n / 10`
done
echo "Reverse of $word is $sum"
done
output: $sh 13.16.sh 123 11
Reverse of 123 is 321
Reverse of 11 is 11
17)write a shell script to display factorial value from given argument list
Shell script:
for word in $*
do
n=1
a=1
while [ $n -le $word ]
do
a=`expr $n \* $a`
n=`expr $n + 1`
done
echo "Factorisal of $word is $a"
done
output: $sh 13.17.sh 6 7
Factorial of 6 is 720
Factorial of 7 is 5040
18)write a shell script which will display maximum file size from the given argument list
Shell script:
b=0
for file in $*
do
a=$(ls -l $file | cut -c29-31)
if [ $a -gt $b ]
then
b=$a
v=$file
fi
done
echo Maximum file size is $b and the file name is $v
output: $sh 13.18.sh 4a.sh 7b.sh 10.c
Maximum file size is 51 and the file name is
10.c
19)write a shell script which will greet you Good Morning, Good Afternoon, Good
Evening and Good Night according to the current time
Shell script:
now="$(date +"%r")"
echo "Time is $now"
v="$(date +"%I")"
a="$(date +"%P")"
if [ $a = "am" ]
then
echo "GOOD MORNING"
fi
if [ $a = "pm" ]
then
if [ $v -lt 4 ]
then
echo "GOOD AFTERNOON"
fi
if [ $v -ge 4 -a $v -le 6 ]
then
echo "GOOD EVENING"
fi
if [ $v -ge 7 ]
then
echo "GOOD NIGHT"
fi
fi
output: $sh 13.19.sh
Time is 06:14:45 PM IST
GOOD EVENING
20)write a shell script to sort the elements in a array using bubble sort technique
Shell script:
echo Enter values into array
read -a array
i=1
size=${#array[@]}
while [ $i -le $size ]
do
p=`expr $size - $i`
j=0
while [ $j -lt $p ]
do
a=`expr $j + 1`
if [ ${array[$j]} -gt ${array[$a]} ]
then
temp=${array[$j]}
array[$j]=${array[$a]}
array[$a]=$temp
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
echo Sorted elements
echo ${array[*]}
output: $sh 13.20.sh
Enter values into array
78 45 12 1 3
sorted elements
1 3 12 45 78
21)write a shell script to find largest element in an array
Shell script:
echo Enter values
read -a array
a=0
for p in ${array[@]}
do
if [ $p -gt $a ]
then
a=$p
fi
done
echo The largest element is $a
output: $sh 13.21.sh
Enter values
1 2 90 3
The largest element is 90
22)write an awk program to print sum, avg of students marks list
Shell script:
BEGIN{print "Name\tTota1\tAverage"}
{
total=$2+$3+$4+$5;
average=(total)/4;
print $1"\t"total"\t"average
}
output:$cat student
Rama 55 65 89 78
Sita 34 56 89 12
satya 36 50 90 68
Swetha 37 67 90 12
$awk f 13.22 student
Name Tota1 Average
Rama 287 71.75
Sita 191 47.75
satya 244 61
Swetha 206 51.5
23)Write an awk program to display students pass/fail report
Shell script:
BEGIN {print "Student pass or fail report"}
{
if($2<35||$3<35||$4<35||$5<35)
print $1"\tFail"
else
print $1"\tPass"
}
output:$cat student
Rama 55 65 89 78
Sita 34 56 89 12
satya 36 50 90 68
Swetha 37 67 90 12
$awk f 13.23 student
Student pass or fail report
Rama Pass
Sita Fail
Satya Pass
Swetha Fail
24)write an awk program to count the no. of vowels from a given file
Shell script:
BEGIN {count=0}
{
col=1
while(col<=NF)
{
len=length($col)
i=1
while(i<=len)
{
s=substr($col,i,1)
if(s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'||s ==
'A'||s =='E'||s =='I'||s =='O'||s =='U')
count=count + 1
}
col=col + 1
}
}
END {print("No of vowels are :"count)}
output:$cat example
welcome to narasaraopeta engineering college
Guntur dist
$awk f 13.24 example
No of vowels are 22
25)write an awk program which will find maximum word and its length in the given input file
Shell script:
BEGIN
{
print("Finds maximum word")
max_len=0
max_word=""
}
{
i=1
while(i<=NF)
{
len_word=length($i)
if(max_len<len_word)
{
max_len=len_word
max_word=$i
}
i++
}
}
END
{
print("Maximum length of word is" max_word)
print("AND ITS length is "max_len)
}
output:$cat example
welcome to narasaraopeta engineering college
Guntur dist
$awk f 13.25 example
Maximum length of word is narasaraopeta
And its length is 13
26)write a shell script to generate mathematical tables
Shell script:
echo "Enter a number"
read num
i=1
while [ $i -le 10 ]
do
a=`expr $num \* $i`
echo "$num * $i = $a"
i=`expr $i + 1`
done
output:$sh 13.26.sh
Enter a number
5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
27)write a shell script to sort elements of given array by using selection sort
Shell script:
echo "enter the array size"
read n
echo "enter numbers in an array"
for((i=0;i<n;i++))
do
read arr[$i]
done
#logic for selection sort
for((i=0;i<n-1;i++))
do
small=${arr[$i]}
index=$i
for((j=i+1;j<n;j++))
do
if((arr[j]<small))
then
small=${arr[$j]}
index=$j
fi
done
temp=${arr[$i]}
arr[$i]=${arr[$index]}
arr[$index]=$temp
done
#printing sorted array
echo "printing sorted array"
for((i=0;i<n;i++))
do
echo ${arr[$i]}
done
output:$sh 13.27.sh
enter the array size
5
enter the numbers in an array
12
9
17
8
1
Printing sorted array
1
8
9
12
17
28)write a shell script to search given number using binary search
Shell script:
echo Enter values into array
read -a array
size=${#array[@]}
echo Enter target
read target
echo size is $size and target is $target
start=0
end=`expr $size - 1`
flag=0
while [ $start -le $end ]
do
middle=`expr $start / 2 + $end / 2`
if [ ${array[$middle]} -eq $target ]
then
flag=1
break
elif [ ${array[$middle]} -lt $target ]
then
start=`expr $middle + 1`
else
end=`expr $middle - 1`
fi
done
if [ $flag -ne 0 ]
then
echo $target is found in the array at $middle position
else
echo $target is not found in the array
fi
output:$sh 13.28.sh
Enter values into array
12 34 56 78 90
Enter target
56
size is 5 and target is 56
56 is found in the array at 2 position
29)write a shell script to find number of vowels,consonants,numbers,white spaces and special
characters in a given string
Shell script:
echo "Input a string"
read s1
length="${#s1}"
echo length of string is $length
i=1
v=0
c=0
n=0
w=0
s=0
while [ $i -le $length ]
do
h=`expr $s1 | cut -c $i`
case $h in
[aeiou])
v=`expr $v + 1`
;;
[AEIOU])
v=`expr $v + 1`
;;

[bcdfghjklmnpqrstvwxyz])
c=`expr $c + 1`
;;

[BCDFGHJKLMNPQRSTVWXYZ])
c=`expr $c + 1`
;;
[0-9])
n=`expr $n + 1`
;;
[" "])
w=`expr $w + 1`
;;
*)
s=`expr $s + 1`
;;
esac
i=`expr $i + 1`
done
echo NO of vowels are $v
echo No of consonants are $c
echo No of digits are $n
echo No of white spaces are $w
echo No of special characters are $s
output:$sh 13.29.sh
Input a string
nec12345!@#$%
length of string is 13
NO of vowels are 1
No of consonants are 2
No of digits are 5
No of white spaces are 0
No of special characters are 5
30)write a shell script to lock terminal
Shell script:
stty .echo
while true
do
clear
echo "Enter the paasword"
read pass1
echo "Re enter the password"
read pass2
if [ $pass1 = $pass2 ]
then
echo "Terminal locked"
echo "To unlock enter the password"
pass1=""
until [ "$pass1" = "$pass2" ]
do
read pass1
done
echo "Terminal unlocked"
stty echo
exit
else
echo "Password mismatch retype it"
fi
done
output:$sh 13.30.sh
Enter the paasword
nec
Re enter the password
nec
Terminal locked
To unlock enter the password
Ne
NEC
nec
Terminal unlocked
$sh 13.30.sh
Enter the paasword
nec
Re enter the password
Ne
Password mismatch retype it

Anda mungkin juga menyukai