unix lab cycle - becbapatla.ac.in lab.pdf · unix lab cycle 1. write a shell script to accept two...
TRANSCRIPT
![Page 1: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/1.jpg)
Unix Lab Cycle
1. Write a shell script to accept two numbers and perform all arithmetic operations on it.
echo “enter two numbers”read aread becho “To perform arithmetic operations enter y otherwise n”read swhile [ $s = y –o $s = Y ]doecho “Menu”echo “1.Addition”echo “2.Subtraction”echo “3.Multiplication”echo “4. Division”echo “5.Modulus”echo “6.Exit”echo “read your choice”read chcase $ch in1)add =`expr $a + $b’
echo “addition is :$add” ;;2) sub =`expr $a - $b’
echo “subtraction is :$sub” ;;3) mul =`expr $a \* $b’
echo “Multiplication is :$mul” ;;4)div =`expr $a / $b`
echo “Division is : $div”;;5) mod =`expr $a % $b`
echo “Modulus is : $mod”;;*)exit
echo “invalid choice”;;esacread sdone
2. Write a shell script to find the largest of three numbers using conditional execution operations.
echo “enter three numbers”read a read bread c
![Page 2: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/2.jpg)
if [ $a -gt $b ]thenif [ $a -gt $c ]then
Echo “ $a is big”else
echo “ $c is big”fielseif [ $b –gt $c ]thenecho “$b is big “else
echo “$c is big “fifi
3. 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 message or not.
echo “enter login name”read st=`who | gerp $s`if test “$t”thenecho “person is login”elseecho “Not login”fip=`mesg | cut –d “ “ –f2`echo “$p”if [ “$p” = “y” ]then
echo “able to receive message”else
echo “not able to receive message”fi
4. Write a shell script to accept the name of the file from standard input and perform the following tests on it.a) File executableb) File readablec) File writeabled) Both readable and writeable.
![Page 3: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/3.jpg)
echo “enter file name”read fecho “do you want to perform operations”read swhile [ $s = y -o $s = Y ]doecho “Menu”echo “1. File executable”echo “2. File read able”echo “3. File writeable”echo “4. Both readable and writeable”echo “enter ur choice”read chcase $ch in 1) if test -x $f
thenecho “file is executable”elseecho “ file is not executable”fi;;
2) if test -r $fthen
echo “file is readable”else
echo “file is not readable “fi;;
3) if test - w $fthenecho “ file is writeable “elseecho “ file is not writeable”fi
;;4) if test -r $f -a -w $f
thenecho “ file is readable and writeable “
elseecho “ not readable and writeable”
fi;;*) exit ;;esacecho “do you want to continue”read wish
![Page 4: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/4.jpg)
if [ $wish = y –o $wish = Y ]thencontinueelseexitfidone
5. Write a shell script which will display the username and terminal name who login recently in the unix system.
echo “user name is :”who | tr –s “ “ | cut -d “ “ -f 1echo “terminal name is :”who | tr -s “ “ | cut -d “ “ –f 2
6. Write a shell script to find no of file in a directory
echo “enter a directory name”read sif test -d $sthen
d =`ls $s | wc -l `echo “No of files are : $d”else
echo “Not a directory”fi
7. Write a shell script to print the following format11 21 2 31 2 3 4
echo “enter a number”read ni =1j =1while [ $i -le $n ]doj =1while [ $j -le $i ]doecho -n “$j”
![Page 5: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/5.jpg)
j =`expr $j + 1`doneecho “ “i =`expr $i + 1`done
8. Write a shell script to print pime numbers up to a given range using arguments.
echo “enter lower and upper limt”if test $# -lt 2thenecho “insufficient arguments”elsel = $1u = $2while [ $l -le $u ]doi =1c =0while [ $i -le $l ]dom=`expr $l % $i `if [ $m –eq 0 ]thenc=`expr $c + 1 `fii = `expr $i + 1`doneif [ $c –eq 2 ]thenecho “ prime number is : $l”fil =`expr $l + 1`done fi
9. Write a shell script which will display the number of days in the given month and year.
echo “enter month”read mecho “enter year”read ycase $m in1) echo “January “
![Page 6: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/6.jpg)
echo “days are :31”;;2) echo “feb”
if [ `expr $y % 4 -eq 0 ]thenecho “days are: 29”elseecho “days are : 28”fi;;
3) echo “March”echo “ days are :31”;;
4 ) echo “April”echo “days are: 30”;;
5) echo “May”echo “ days are : 31”;;
6) echo “June”echo “days are: 30”;;
7) echo “July”echo “ days are: 31”;;
8) echo “August”echo “ days are: 31”;;
9) echo “Sep”echo “days are: 30”;;
10) echo” Oct”echo “ days are : 31”;;
11) echo “Nov”echo “days are: 30”;;
12 ) echo “Dec”echo “ Days are: 31”;;
*) echo “invalid choice”;;esac
10. Write a shell script to check whether a given number is perfect or not.
echo “enter a number”read ni =1s =0m = $nwhile [ $i -lt $n ]dox =`expr $n % $i`if [ $x –eq 0]
![Page 7: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/7.jpg)
thens = `expr $s + $i `fii = `expr $i + 1`doneif [ $s –eq $m]thenecho “given no is perfect”elseecho “given number is not perfect”
11. Write a shell script to copy, rename, edit and delete a file.
echo “do you want to perform operations:”read swhile [ $s = y –o $s = Y ]doecho “enter a file”read fecho “Menu”echo “1. Copy”echo “2. Edit”echo “3. Rename”echo “4.Delete”echo “5.Exit”echo “enter ur choice”read chcase $ch in1) echo “enter another file to copy:”
read f1cp $f $f1echo “file is copied”;;
2) echo “ enter file name to which data is appended”cat >> $fecho “after editing”cat $f;;
3) echo “ Rename the file”read f1mv $f $f1echo “file moved”;;
4) rm $fecho “ file is deleted”;;
![Page 8: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/8.jpg)
*) echo “invalid choice”;;esacecho “ Do u want to continue”read aif [ $a = y –o $a = Y ]thencontinueelse exitfidone
12. Write a shell script for concatenation of two strings”
echo “enter two strings”read sread s1echo “concatenation is : $s $s1”
13. Write a shell script which will display Fibonacci series up to a given number of arguments.
If test $# -lt 1thenecho “insufficient arguments”elsen= $1count=0f1=0f2=1echo “series is :”echo “$f1”echo “$f2”while [ $count –le $n ]dof=`expr $f1 + $f2`f1= $f2f2= $fecho “$f”count=`expr $count + 1`done fi
![Page 9: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/9.jpg)
14. Write a shell script to accept student number ,name , marks in 5 subjects . Find total , average and grade. Display the result of student and store in a file called stu.dat Rules are:Avg >=80 then grade AAvg <80 && avg >=70 then grade BAvg <70 && avg >=60 then grade CAvg <60 && avg>=50 then grade DAvg <50 && avg >=40 then grade EElse grade F
echo “Do you want to enter student data”read awhile [ $a = y -o $a = Y ]doecho “enter number”read snoecho “enter name”read nameecho “enter 5 dubject marks”read m1read m2read m3read m4read m5total = `expr $m1 + $m2 + $m3 + $m4 + $m5`echo “total is $total”avg = `expr $total / 5`echo “average is $avg”if [ $avg –ge 80 ]thengrade = “A”elif [ $avg –lt 80 -a $avg -ge 70 ]thengrade = “B”elif [ $avg -lt 70 -a $avg -ge 60 ]thengrade = “C”elif [ $avg -lt 60 -a $avg -ge 50 ]thengrade = “D”elif [ $avg -lt 50 –a $avg –ge 40 ]thengrade = “E”elsegrade = “F”fi
![Page 10: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/10.jpg)
echo “enter filename”read fecho “ $sno $name $total $avg $grade >> $fecho “Do you want to continue”read adone
First create the file with name stu.dat in the folloeing format.
cat > stu.datStudent InformationSNO NAME TOTAL AVERAGE GRADE
Press ctrl –d
15. Write a shell script to accept empno, empname, basic. Find DA, HRA, TA, PF, GROSS SAL and NETSAL. Also store all details in a file called emp.dat The rules are: HRA is 18% of basic if basic >5000 otherwise 550.
DA is 35% of basic.PF is 13% of basicIT is 14 % of basicTA is 10% of basic.
echo “Do you want to enter employee data”read awhile [ $a = y -o $a = Y ]doecho “enter employee number”read snoecho “enter employee name”read nameecho “enter basic salry”read bsalif [ $bsal -gt 5000 ]thenk =`expr 18 \* $bsal`hra=`expr $k / 100`elsehra=550fik = `expr 15 \* $bsal`da= `expr $k / 100`k = `expr 13 \* $bsal`pf = `expr $k /100`
![Page 11: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/11.jpg)
k =`expr 10 \* $bsal`ta=`expr $k / 100`k=`expr 14 \* $bsal`it = `expr $k \ 100`gsal = `expr $hra + $da + $ta + $it + $bsal + $pf’nsal= `expr $gsal - $pf - $it`echo “enter file name”read fecho “EMPLOYEE DETAILS ARE:”>> $fecho “ EMPNO is: $sno >> $fecho “EMPLOYEE NAME IS: $name “>> $fecho “ BASIC SALARY IS: $bsal “ >>$fecho “ GROSS SALARY IS : $gsal “>> $fecho “ NETSALARY IS: $nsal “>>$fecho “Do you want to continue”read adone
16. Write a shell Script to demonstrate break and continue statements.
echo “Testing for break”i= 1while [ $i -le 10 ]doecho “ $i”i=`expr $i + 1`if [ $i –eq 5 ]thenbreakfidoneecho “ testing for continue”i=0while [ $i –le 10 ]doi=`expr $i + 1`if [ $i –eq 5 ]then continuefiecho “ $i”done
17. Write a shell script to satisfy the following menu optiions.
![Page 12: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/12.jpg)
a) Dislpay current directory pathb) Display todays datec) Display users who are connected to the unix systemd) Quit
echo “enter ur choice”echo “MENU”echo “1.PATH”echo “2. DATE”echo “3.WHO”echo “4.QUIT”read chcase $ch in1) echo “ Current directory is “
pwd;;2) echo “Cureent date is :”
date;;3) echo “Users connected to unix system is:”
who ;;4) exit;;esac
18. Write a shell script to delete all files whose size is zero bytes from current directory.
echo “enter a directory name”read aif test -d $athen
echo “it is directory”cd $aecho “before deleting the files are:”
ls -lfor k in `ls -l | tr –s “ “ | cut -d “ “ -f 5,9 |grep 0 | cut -d “ “ -f 2`dorm $kecho “Deletion is done and remaining files are:”ls -ldoneelseecho “$d is not directory”fi
19. Write a shell script to display string palindrome from given argumnets.
![Page 13: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/13.jpg)
if $# -lt 1thenecho “Insufficeint argumnets”exit ficount =`echo $1 | wc -c`input = $1while [ $count -gt 0 ]dovar =`echo $1 | cut –c $count `count = `expr $count - 1`temp= `echo $temp$var`doneif [ $input = $temp ]thenecho “String is palindrome”elseecho “not palindrome”fi
20. Write a shell script which will display armstrong numbers from given argument.
if test $# -lt 1thenecho “Insufficient arguments”elsei= $1n= $2while [ $i -le $n ]dos=0m=$iwhile [ $i –gt 0]dor=`expr $i % 10`d=`expr $r \* $r \* $r`s=`expr $s + $d`i=`expr $i / 10`doneif [ $i –eq $m ]thenecho “given number is armstrong $m “fii=`expr $i + 1`
![Page 14: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/14.jpg)
donefi
21. Write a shell script to display a revere of a number from given arguments.
if test $# -lt 1thenecho “Insufficient arguments”elsen= $1s=0while [ $n -gt 0]don=`expr $n % 10`s=`expr $s \* 10 + $m`n=`expr $n / 10`doneecho “reverse number is : $s”fi
22. Write a shell script to display factorial value from given argument list.
if test $# -lt 1thenecho “Insufficient arguments”elsen= $1i=1f=1while [ $i -le $n ]dof=`expr $f \* $i`i=`expr $i + 1`doneecho “fact is $f”fi
23. Write a shell script which will find maximum file size in the given argumnet list.
max =0for k in `ls -l | tr -s “ “ | cut -d “ “ -f 5`do
![Page 15: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/15.jpg)
if [ $ k -gt $max ]thenmax = $kelsemax= $maxfidonefile= `ls -l | grep $max | tr -s “ “ | cut -d “ “ -f 9`echo “the file $file is having amximum size $max”
24. Write a shell script which will greet you “Good Morning” , “ Good Afternoon “, “Good Evening “ and “Good Night” according to current time.
t = `Date +%H`if [ $t -lt 12 ]thenecho “Good Morning”elif [ $t -le 16 ]thenecho “Good Afternoon”elif [ $t -lt 20]thenecho “Good Evening “elseecho “Good Night”
25. Write a shell script which will display total size of directories.
echo “enter a directory”read sif test -d $sthenls -l $s | head -1elseecho “It is not directory”fi
26. Write a shell script to sort the elements in an array using bubble sort technique.
echo “enter size:”read necho “enter elements”i=0while [ $i -le $n ]do
![Page 16: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/16.jpg)
read pa[$i] = $pi=`expr $i + 1`donej=0while [ $j -le `expr $n - 1` ]dok=0while [ $k -le `expr $n - $j -1` ]doif [ ${a[ $k]} -gt ${a[ $k + 1]} ]thent= $ { a[ $k] }a[ $k ] =${a[ $k + 1] }a[ $k + 1] =$tfik=`expr $k + 1`donej=`expr $j + 1`donel=0echo “elements after sorting”while [ $l -le $n ]doecho –n –e ${a[$l]} “\t”l=`expr $l + 1`doneecho “ “
27. Write a shell script to find largest element in an array.
echo “enter size:”read necho “enter elements”i=0while [ $i -le $n ]doread pa[$i] = $pi=`expr $i + 1`dones= ${a[ $i]}while [ $i -le $n]doj=`expr $i + 1`if [ $s –lt ${a[$j]} ]
![Page 17: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/17.jpg)
thens=${a[ $i]}fii=`expr $i + 1`doneecho “largest element is $s”
28. Write an awk program for display the lines in any file center alignment.
echo “enter file name”read fcat $f | awk ‘ {
for (k=1;k<35;k++)printf “%s” , “ “print $0 }’
29. Write an awk program to print sum ,avg of student marks list
echo “ Enter file name”read fawk -F “ “ ‘ {
print $1, $2, $3 ,$4} ‘ $f
30. Write an awk program to display total number of users and their names in unix system.
echo “to count no of users and their names”who | wc -l | awk ‘ { print “No of users are:” , NF}who | awk ‘ { print “ Names of users are : “ , $1}
31. Write an awk program to display student pass/fail report.
echo “ Enter file name”read fawk -F “ “ ‘ {
print $1, $2 ,$5} ‘ $f
32. Write an awk program to count no of vowels in agiven file.
awk ‘ BEGIN {
String=”this is unix lab”Search = “ “
![Page 18: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/18.jpg)
s1 = “aeiou”n = split(String, a,search);n1=split(s1,b,search);for (i =1;i<=n;i++){
printf(“word [ %d]= %s\t”, i ,a[i]);}for (i =1;i<=n;i++){
printf(“words [ %d]= %s\t”, i ,a[i]);}v=0;for (i =1;i<=n;i++){
for (j =1;j<=n;j++){
if (a[i]==b[j]){
v= v+1;}
}}
printf( “no of vowels are %d” , v);exit;}’
33. Write an awk program which will find maximum word and its length in the given input file.
awk ‘ BEGIN {
String=”this is unix lab”Search = “ “n = split(String, a,search);for (i =1;i<=n;i++){
printf(“word [ %d]= %s\t”, i ,a[i]);}for (i=1;i<=n;i++){
m=length(a[i[);for (j=i+1;j<=n;j++){
p=length(a[j]);
![Page 19: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/19.jpg)
if (m>p){
max=m;k=a[i];
}else{
max=p;k=a[j];
}}
}printf(“the maximum word in a file is %s “ “and having length %d \n “ ,k , max);
exit;}’
34. Write a shell script to generate mathmatical table
echo “enter a number”read ni=1;while [ $i –le 10 ]dob=`expr $n \* $i`echo “$a \* $i = $b”i=`expr $i + 1`done
35. Write a shell script to check whether a given number is strong or not.
echo “enter anumber”read ns=0;m= $nwhile [ $n –gt 0 ]doi=1f=1d=`expr $n % 10`while [ $i -le $d ]dof=`expr $f \* $i`i=`expr $i + 1`dones=`expr $s + $f`
![Page 20: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/20.jpg)
n=`expr $n / 10`doneif [ $m -eq $s ]thenecho “ given number is strong “elseecho “not strong”fi
36. Write a shell script to sort elements in an array using selection sort.
echo “enter size:”read necho “enter elements”i=0while [ $i -le $n ]doread pa[$i] = $pi=`expr $i + 1`donej=0while [ $j -lt $n ]dok=`expr $j + 1`while [ $k -lt $n ]doif [ ${a[$k] } -lt ${a[ $j]} ]thent=${a[$k]}a[$k]= ${a[$j]}a[$j]=$tfik=`expr $k + 1`donej=`expr $j + 1`done s=0echo “enter elements after sorting”while [ $s –lt $n ]doecho -n -e ${a[$s]} “\t”s=`expr $s + 1`done
![Page 21: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/21.jpg)
37. Write a shell script to search a given number using binary search.
echo “enter size:”read necho “enter elements”i=0while [ $i -le $n ]doread pa[$i] = $pi=`expr $i + 1`donej=0while [ $j -lt $n ]dok=`expr $s – 1`while [ $k –lt $s]doif [ ${a[$k]} -lt ${a[$j]} ]thent=${a[$k]}a[$k]= ${a[$j]}a[$j]=$tfik=`expr $k + 1`donej=`expr $j + 1`done echo “enter element to search”read eh=0l= $swhile [ $h –le $l ]domid =`expr $l + $h`mid1=`expr $mid / 2`if [ $e -lt ${a[$mid1]} ]thenl = $mid1elif [ $e -gt ${a[$mid1]} ]thenh=$mid1elseecho “found at $mid1”
![Page 22: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/22.jpg)
breakfidone
38. Write a shell script to find no of vowels, consonants, numbers, white spaces and special characters in a given string.
echo “ enter file name”read fecho “enter string in file”cat >$fn=`wc –c $f | cut -d “ “ -f 1`i =1while [ $i -le $n ]doa[ $i]= `cut -c $i $f`i=`expr $i + 1`donevc=0wc=0nc=0sc=0cc=0k=1while [ $k - le $n ]dou=${a[$k]}case $u in
[a-z]) if [ “$u” = “a” –o “$u” = “e” -o “$u” = “i” -o “$u” = “o” -o “$u” = “u” ]then
vc =`expr $vc + 1`else
cc=`expr $cc + 1`fi;;[0-9] ) nc =`expr $nc + 1`;;*) if test –z $u
thenwc=`expr $wc + 1`elsesc=`expr $sc + 1`fi;;
esac
![Page 23: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/23.jpg)
echo “ No of vowels are : $vc”echo “No of consonants are : $cc”echo “No of white spaces are: $wc “echo “ No of special symbols are: $sc”echo “No of digits are : $nc”
39. Write a shell script to lock the terminal.
Clearecho “enter password”stty –echo read p1stty echo echo “confirm password”stty -echoread p2stty echoif [ “ $p1” != “$p2” ]thenecho “wrong password”exitficlear echo “screen locked”echo “ to open the lock enter a password”stty -echoread p3sty echowhile [ “$p3” != “$p1” ]doecho “wrong password”echo “enter password again”stty -echoread p3stty echodoneclearecho “screen is unlocked”exit
40. Write a shell script which merge the contents of file1, file, file3 sort them and display the sorted output on the screen page by page.
echo “enter file names”
![Page 24: Unix Lab Cycle - becbapatla.ac.in LAB.pdf · Unix Lab Cycle 1. Write a shell script to accept two numbers and perform all arithmetic operations on it. echo “enter two numbers”](https://reader031.vdocuments.net/reader031/viewer/2022030506/5ab4441b7f8b9a0f058b9ff1/html5/thumbnails/24.jpg)
read f1read f2read f3echo “the merged file is:”read f4cat $f1 $f2 $f3 >> $f4more -c $f1 $f2 $f3