2.a. Write a shell script to accept numbers and perform addition, subtraction, division and multiplication.

echo "Enter Number"

read a

echo "Enter Number"

read b

c=`expr $a + $b`

echo "Add: " $c

C=`expr $a - $b`

echo "Sub: " $c

c=`expr $a / $b`

echo "Div: " $c

c=`expr $a \* $b`

echo "Mul : " $c


2.b. Write a shell script to accept the string and checks whether the string is palindrome or not.

clear

echo "Enter String :"

read s

l=`echo $s|wc -c`

l=`expr $l - 1`

echo "length of String : "$l

i=1

while [ $i -le $l ]

do

rstr=`echo $s|cut -c$i`$rstr

i=`expr $i + 1`

done

if [ "$rstr" = "$s" ]

then

echo "Your string is palindrome"

else

echo "Your string is not palindrome"

fi


2.c. Write a shell script to Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.

clear
echo "Enter Number : "
read num
cnt=0
tot=0
if [ `expr $num % 2` -eq 0 ]
then
echo "NUMBER IS EVEN"
else
echo "NUMBER IS ODD"
fi
while [ $num -ne 0 ]
do
n=`expr $num % 10`
num=`expr $num / 10`
tot=`expr $tot + $n`
cnt=`expr $cnt + 1`
done
echo "Sum of All Digit : $tot"
echo "Total No. of Digit : $cnt"





2.d. Accept strings and replace a string by another string
.

clear
echo "Enter string : \c"
read s
echo "\n\nWhat you want to search from [ $s ] :"
read ss
echo "\n\nEnter string to replace : \c"
read rs
s=`echo $s | sed s/$ss/$rs/`
echo "\n\nstring replaced successfully \n New string is : $s "

2.e. Write a shell script to accept filename and displays last modification time if file exists, otherwise display appropriate message.

clear

echo "Enter filename"

read fname

if [ -f $fname ]

then

echo "Modification time of [ $fname ] is \c "

ls -l $fname | cut -c 30-38

else

echo "file [ $fname ] not exist in \c "

pwd

fi

2.f. write a shell script to fetch the data from a file and display data into another file in reverse order.



clear
echo "Enter File Name : \c "
read fname
if [ -f $fname ]
then
str=`cat $fname`
l=`echo $str|wc -c`
i=$l
while [ $i -ge 1 ]
do
t=$t`echo $str|cut -c $i`
i=`expr $i - 1`
done
echo $t>Result_ex2f.txt
cat Result_ex2f.txt
else
echo "file [ $fname ] not exist in \c "
pwd
fi

3. Write a shell script to find the global complete path for any file.

clear
echo "Enter File Name : \c "
read fname
if [ -f $fname ]
then
str=`find $fname`
path=`pwd`
echo "Full path of file is $path/$str"
else
echo "file [ $fname ] not exist in \c "
pwd
fi


4. Write a script to broadcast a message to a specified user or a group of users logged on any terminal.


clear

echo "Enter Username : "

read user

echo "Enter message and Ctrl + d for brodcast : "

write $user

echo "Message Sent !"


5. Write a script to copy the file system from two directories to a new directory in such a way that only the
latest file is copied in case there are common files in both the directories.


clear

echo "Enter First Dir name : \c"

read fd

echo "Enter Second Dir name : \c"

read sd

if [ ! -d $fd ]

then

echo "Directory $fd not avaliable"

fi

if [ ! -d $sd ]

then

echo "Directory $sd not avaliable"

fi

ls $fd > fd.lst

ls $sd > sd.lst

if [ ! -d Traget ]

then

mkdir Traget

fi

for FileFromDir1 in `cat fd.lst`

do

fleg=0

for FileFromDir2 in `cat sd.lst`

do


if [ "$FileFromDir1" = "$FileFromDir2" ]

then


flag=1

recoard=`ls -lt $fd/$FileFromDir1 $sd/$FileFromDir2 | head -n 1`

filepath=`echo $recoard | cut -d " " -f 9`

cp $filepath Traget


fi

done

done

if [ $flag -eq 0 ]

then

cp $fd/$FileFromDir1 Traget

fi

for FileFromDir2 in `cat sd.lst`

do

flag=0

for FileFromDir1 in `cat fd.lst`

do

if [ "$FileFromDir2" = "$FileFromDir1" ]

then

flag=1

fi

done

if [ $flag -eq 0 ]

then

cp $sd/$FileFromDir2 Traget

fi

done

echo "Successfuly Copied !"
6. Write a script to compare identically named files in two different directories and if they are same, copy
one of them in a third directory.


clear

echo "Enter First Dir name : \c"

read fd

echo "Enter Second Dir name : \c"

read sd

if [ ! -d $fd ]

then

echo "Directory $fd not avaliable"

fi

if [ ! -d $sd ]

then

echo "Directory $sd not avaliable"

fi

ls $fd > fd.lst

ls $sd > sd.lst





if [ ! -d NewTraget ]

then

mkdir NewTraget

fi





for FileFromDir1 in `cat fd.lst`

do

fleg=0

for FileFromDir2 in `cat sd.lst`

do


if [ "$FileFromDir1" = "$FileFromDir2" ]

then


flag=1

recoard=`ls -lt $fd/$FileFromDir1 $sd/$FileFromDir2 | head -n 1`

filepath=`echo $recoard | cut -d " " -f 9`

cp $filepath NewTraget

fi

done

done

echo "Successfuly Copied !"

7. Write a script to delete zero sized files from a given directory (and all its sub-directories).



clear

echo "Enter Dir name:\c"

read dr

if [ ! -d $dr ]

then

echo "Directory not avaliable."

fi

cd $dr



ls $dr > dr.lst



f=0

d=0



for file in `cat dr.lst`

do

if [ ! -d $file ]

then

size=`ls -s $file`

size=`echo $size | cut -d " " -f 1`

fi



if [ $size -eq 0 ]

then

echo "demo"

f=`expr $f + 1`

rm $file

else

d=`expr $d + 1`

rm -r $file

fi

done


8. Write a script to display the name of those files (in the given directory) which are having multiple links.


clear
echo "enter string : "

read x

char=`echo $x | wc -c`

char=`expr $char`

echo "Charecter are : $char"


while [ "$n -lt $y ]

do

x=`expr $x \* $z`

n=`expr $n + 1`

done



9. Write a script to display the name of all executable files in the given directory.



echo "enter string : "

read x

char=`echo $x | wc -c`

char=`expr $char`

echo "Charecter are : $char"

while [ "$n -lt $y ]

do

x=`expr $x \* $z`

n=`expr $n + 1`

done


10.Write a script to display the date, time and a welcome message (like Good Morning
should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.


clear

echo "Enter String : "

read x

char=`echo $x | wc -c`

s=1

vw=1

vowel='a'

while [ $s -ne $char ]

do

ans=`echo $x | cut -c $s`

if [ $ans -eq $vowel ];then

vw=`expr $vw + 1`

echo $vw

echo $ans

echo $vowel

fi

s=`expr $s + 1`

done

11. Write a script to display the directory in the descending order of the size of each file.


clear

echo "Enter Dir name :\c"

read dr

ls -lSr $dr


12. Write a script to implement the following commands:

Tree (of DOS)

which (of UNIX)


clear


echo "Enter String : "

read x

char=`echo $x | wc -c`


s=0

while [ $char -ne $s ]

do

ans=`echo $x | cut -c $char`

echo $ans

char=`expr $char - 1`

done


13. Write a script for generating a mark sheet after reading data from a file.

File contains student roll no, name , marks of three subjects.
etc.). The time.

clear

len=`cat studInfo.txt | wc -l`

i=1

echo "\n\n\t\t\t [ STUDENT MARKSHEET ]\n"

echo "__________________________________________________________________________"

echo "#\t NAME \t \t \t TOTAL \t \t PERCENTAGE \t GRADE "

echo "__________________________________________________________________________"



while [ $i -le $len ]

do

record=`head -n $i studInfo.txt | tail -n 1`

total=0

j=2

isFail=0

while [ $j -le 4 ]

do

marks=`echo $record | cut -d "," -f $j`

if [ $marks -lt 40 ]

then

isFail=1

fi

total=`expr $marks + $total`

j=`expr $j + 1`

done

name=`echo $record | cut -d "," -f 1`

per=`expr $total / 3`



if [ $isFail = 0 ]

then

if [ $per -ge 85 ] && [ $per -le 100 ]

then

grade="AA"

elif [ $per -ge 75 ] && [ $per -le 84 ]

then

grade="AB"

elif [ $per -ge 65 ] && [ $per -le 74 ]

then

grade="BB"

elif [ $per -ge 55 ] && [ $per -le 64 ]

then

grade="BC"

elif [ $per -ge 45 ] && [ $per -le 54 ]

then

grade="CC"

else

grade="FF"

fi

else

grade="FF"


fi



echo "$i \t $name \t \t $total \t \t $per % \t \t $grade"

i=`expr $i + 1`

done

echo "__________________________________________________________________________\n"


14. Write a script to make following file and directory management operations menu based:

Display current directory

List directory

Make directory

Change directory

Copy a file

Rename a file

Delete a file

Edit a file


clear

echo "Enter First name : "

read fn

echo "Enter Middle name : "

read mn

echo "Enter Last name : "

read lastn

ans=`echo $fn $mn $lastn > out.txt`




15. Write a script which reads a text file and output the following

Count of character, words and lines.

File in reverse.

Frequency of particular word in the file.

Lower case letter in place of upper case letter.



clear

echo "Enter file name : "

read fn

ans=`echo $fn | cat $fn`

echo $ans

16. Write a shell script to check whether the named user is currently logged in or not.


clear

echo "Enter username :\c"

read usn

who am i > x.var

ans=`cat x.var | cut -d " " -f 1`

if [ $ans = $usn ]

then

echo "$usn logged in."

else

echo "$usn not logged in."

fi
17. Write a Script for Simple Database Management System Operation.

Database File Contains Following Fields.

EMP_NO

EMP_NAME

EMP_ADDRESS

EMP_AGE

EMP_GENDER

EMP_DESIGNATION

EMP_BASIC_SALARY

Provide Menu Driven Facility For

VIEW RECORD BASED ON QUERY

ADD RECORD

DELETE RECORD

MODIFY RECORD.

COUNT TOTAL NUMBER OF RECORDS

EXIT



clear

AutoNumber()

{

local eno=0

f=0

for j in `cat Employee.txt`

do

eno=$(echo "$j" | cut -d "," -f 1)

f=1

done

if [ $f = 1 ]

then

eno=`expr $eno + 1`

else

eno=1

fi

echo $eno

}


Insert()

{

clear

eno=$1

echo "Enter Employee No: $eno"

echo "Enter Employee Name: \c"

read enm

echo "Enter Employee Address: \c"

read eadd

echo "Enter Employee Age : \c"

read eage

echo "Enter Employee Gender: \c"

read egen

echo "Enter Employee Designation : \c"

read edes

echo "Enter Employee Basic Salary : \c"

read ebal

echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true" >> Employee.txt



echo "Insert Sucessfully"

}

Display()

{

clear

echo "__________________________________________________"

echo " Employee Details "

echo "__________________________________________________"

echo "__________________________________________________"

echo "#ENO \t ENAME \t\t EADDR \t\t\t EAGE \t EGEN \t EDES \t\t EBAL"

for j in `cat Employee.txt`

do

eno=$(echo "$j" | cut -d "," -f 1)

enm=$(echo "$j" | cut -d "," -f 2)

eadd=$(echo "$j" | cut -d "," -f 3)

eage=$(echo "$j" | cut -d "," -f 4)

egen=$(echo "$j" | cut -d "," -f 5)


edes=$(echo "$j" | cut -d "," -f 6)

ebal=$(echo "$j" | cut -d "," -f 7)

tfval=$(echo "$j" | cut -d "," -f 8)

if [ $tfval = "true" ]

then

echo "___________________________________________"

echo "$eno \t $enm \t\t $eadd \t\t $eage \t $egen \t $edes \t $ebal"

fi

done

echo "__________________________________________________"
}
Search()

{

clear

echo "Enter Employee NO: \c"

read no

echo "__________________________________________________"

echo " Employee Details "

echo "__________________________________________________"
flag=0

for j in `cat Employee.txt`

do

eno=$(echo "$j" | cut -d "," -f 1)

enm=$(echo "$j" | cut -d "," -f 2)

eadd=$(echo "$j" | cut -d "," -f 3)

eage=$(echo "$j" | cut -d "," -f 4)

egen=$(echo "$j" | cut -d "," -f 5)

edes=$(echo "$j" | cut -d "," -f 6)

ebal=$(echo "$j" | cut -d "," -f 7)

tfval=$(echo "$j" | cut -d "," -f 8)
if [ $no -eq $eno ] && [ $tfval = "true" ]

then

flag=1

echo "________________________________________"

echo " ENo : $eno EName : $enm "

echo "________________________________________"

echo " EAdd : $eadd "

echo " EAge : $eage "

echo " EGen : $egen "

echo "________________________________________"

echo " EDes : $edes "

echo "________________________________________"

echo " ESal : $ebal "

echo "________________________________________"

fi

done

if [ $flag = 0 ]

then

echo " No Record Found "

fi

echo "__________________________________________________"



}

Delete()

{

clear

f=0

echo "Enter Employee NO: \c"

read no

for j in `cat Employee.txt`

do

eno=$(echo "$j" | cut -d "," -f 1)

enm=$(echo "$j" | cut -d "," -f 2)

eadd=$(echo "$j" | cut -d "," -f 3)

eage=$(echo "$j" | cut -d "," -f 4)

egen=$(echo "$j" | cut -d "," -f 5)

edes=$(echo "$j" | cut -d "," -f 6)

ebal=$(echo "$j" | cut -d "," -f 7)

if [ $no -eq $eno ]

then

f=1
line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,false")

fnm=`cat Employee.txt`

d=$(echo "$fnm" | sed s/$j/$line/g )

echo $d > Employee.txt

echo " Delete Successfully "

fi

done

if [ f = 0 ]

then

echo " No Record Found "

fi

}



Update()

{

clear

echo "Enter Employee NO: \c"

read no


for j in `cat Employee.txt`

do

eno=$(echo "$j" | cut -d "," -f 1)

enm=$(echo "$j" | cut -d "," -f 2)

eadd=$(echo "$j" | cut -d "," -f 3)

eage=$(echo "$j" | cut -d "," -f 4)

egen=$(echo "$j" | cut -d "," -f 5)

edes=$(echo "$j" | cut -d "," -f 6)

ebal=$(echo "$j" | cut -d "," -f 7)


if [ $no -eq $eno ]

then

echo "______________Enter New Record______________"

echo "Enter Employee No: $eno"


echo "Enter Employee Name: \c"

read enm


echo "Enter Employee Address: \c"

read eadd


echo "Enter Employee Age : \c"

read eage


echo "Enter Employee Gender: \c"

read egen


echo "Enter Employee Designation : \c"

read edes



echo "Enter Employee Basic Salary : \c"

read ebal




line=$(echo "$eno,$enm,$eadd,$eage,$egen,$edes,$ebal,true")



#line=$(echo "$eno,$snm,$m1,$m2,$m3,$total,$per,true")

fnm=`cat Employee.txt`

d=$(echo "$fnm" | sed s/$j/$line/g )

echo $d > Employee.txt
echo”Update Sucessfully”

fi
done

}

while [ true ]

do

echo " _______________________________"

echo " 1. Insert "

echo " 2. Delete "

echo " 3. Update "

echo " 4. Display "

echo " 5. Search "

echo " 6. Exit "

echo " _______________________________"

echo "Enter Choice: \c"

read ch

case $ch in

1)
nxtSrNo=$(AutoNumber)

Insert $nxtSrNo
;;

2) Delete ;;

3) Update ;;

4) Display ;;

5) Search ;;

6) break;;

*) echo " Wrong Choice "

esac

done
18. Write A Script To Perform Following String Operations Using Menu:

COMPARE TWO STRINGS.

JOIN TWO STRINGS.

FIND THE LENGTH OF A GIVEN STRING.

OCCURRENCE OF CHARACTER AND WORDS
REVERSE THE STRING.



clear

choice=y

while [ "$choice" = "y" ]

do

echo "____________________________________________"

echo "1. COMPARE TWO STRINGS"

echo "2. JOIN TWO STRINGS"

echo "3. FIND THE LENGTH OF A GIVEN STRING"

echo "4. OCCURRENCE OF CHARACTER AND WORDS"

echo "5. REVERSE THE STRING"

echo "6. EXIT"

echo "____________________________________________"

echo "Enter Choice: \c"

read ch

echo "____________________________________________"

case $ch in

1)
echo "Enter String1: \c"

read str1

echo "Enter String2: \c"

read str2



if [ $str1 = $str2 ]

then

echo "String is equal"

else

echo "String is not equal"

fi

;;

2)
echo "Enter String1: \c"

read str1

echo "Enter String2: \c"

read str2



str3=$str1$str2

echo "Join String: \c" $str3

;;

3)
len=0

echo "Enter String1: \c"

read str1

len=$(echo "$str1" | wc -c)

len=`expr $len - 1`

echo "Length: " $len

;;

4)
echo "Enter String: \c"

read str



echo "Enter Word to find : \c"

read word



echo $str | cat > str1.txt

grep -o $word str1.txt | cat > str2.txt

count=`grep -c $word str2.txt`

echo "Count: \c"$count

;;

5)
echo "Enter String1: \c"

read str

len=`expr $str | wc -c`

len=`expr $len - 1`

while [ $len -gt 0 ]

do

rev=`expr $str | cut -c $len`

ans=$ans$rev

len=`expr $len - 1`

done

echo "Reverse String: \c"$ans

;;

6) exit ;;



*) echo "Invalid Choice ....." ;;

esac

echo "Do u want to continue.....? [y/n]"

read choice

case $choice in

Y|y) choice=y;;

N|n) choice=n;;

*) choice=y;;

esac

done


19. Write a script to calculate gross salary for any number of employees

Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.


clear

echo "Enter Following things"

echo " "

echo "Basic :\c"

read basic

echo $basic > salary.txt

out=`cat < salary.txt`

hrd=`expr $out \* 10 / 100`

da=`expr $out \* 15 / 100`

grosssalary=`expr $out + $hrd + $da`

echo "Basic : " $out

echo "HRD : " $hrd

echo "DA : " $da

echo "Gross Salary : "$grosssalary

20. Write a script to check whether a given string is palindrome or not.


clear

echo "Enter String : \c"

read s

p=$s

m=`expr $s | wc -c`

i=`expr $m - 1`

while [ $i -ge 1 ]

do

k=`expr $s | cut -c$i`

r=$r$k

i=`expr $i - 1`

echo $k

done

echo $r

if [ "$p" != "$r" ]

then

echo "It is not palindrome"

else

echo "It is palindrome"

fi



21. Write a script to check whether a given number is palindrome or not.

clear

echo "Enter No : \c"

read no

m=$no

rev=0

while [ $no -gt 0 ]

do

r=`expr $no % 10`

rev=`expr $rev \* 10 + $r`

no=`expr $no / 10`

done



if [ $m = $rev ]

then

echo " [ $m] is Palindrome"

else

echo " [ $m ] is not Palindrome"

fi


22. Write a script to display all words of a file in ascending order.


echo "Enter File name:\c"

read fn

for i in `cat $fn`

do

echo $i >> f2.txt

done

sort f2.txt

rm f2.txt



23. Write a script to display all lines of a file in ascending order.


clear

echo "Enter File Name :\c"

read filename

sort $filename




24. Write a script to display the last modified file.




clear

echo "Enter Directory Name :\c"

read dir1

cd $dir1

ls -lt | head -2 | tail -1 |cut -d " " -f 10


25. Write a shell script to add the statement #include <stdio.h> at the beginning of every C source file in
current directory containing printf and fprintf.



clear



grep -l -e "printf" -e "fprintf" *.c > temp.txt



#grep

# -l, --files-with-matches

# Suppress normal output; instead print the name of each input

# file from which output would normally have been printed.



# -e PATTERN, --regexp=PATTERN

# Use PATTERN as the pattern. This can be used to specify

# multiple search patterns





for i in `cat temp.txt`

do

sed '1i\#include<stdio .h=".h">' $i > temp2.txt

cat temp2.txt > $i

echo "Successfully Added..."

done



# sed i :

# -i[SUFFIX], --in-place[=SUFFIX]

# edit files in place (makes backup if extension supplied)





# sed l :

# -l N, --line-length=N

# specify the desired line-wrap length for the `l' command </stdio>



26. Write a script that behaves both in interactive and non-interactive mode. When no arguments are
supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts
for deletion of the file. If the user supplies arguments with the script, then it works on those files only.


clear
flag=1

if [ $# -ne 0 ]

then

# if user provide arguments

echo $* >temp

flag=1

else

# if user not provide arguments

ans=`ls *.c`

if [ "" == "$ans" ]

then

echo "There are no c file"

flag=0

else

ls *.c>temp

flag=1

fi

fi

if [ $flag -eq 1 ]

then

for filename in `cat temp`

do

if [ -f $filename ]

then

echo "_______________ $filename _______________"

sed -n -e "1,10 p" $filename

rm -i $filename

fi

done

rm temp

fi






27. Write a script that deletes all leading and trailing spaces in all lines in a file. Also remove blank lines
from a file. Locate lines containing only printf but not fprintf.

clear
echo "Enter file name : "
read fileName
if [ -f $fileName ]
then
echo "____________________________________"
echo " Trim File .... "
echo "____________________________________"

# Delete leading & trailing whitespace from each line and store it tempFile.txt
sed 's/^[ \t]*//;s/[ \t]*$//;' $fileName >tempFile.txt

# Remove all blank lines from tempFile.txt
sed '/^$/d' tempFile.txt

# Replace content of this two file
mv tempFile.txt $fileName
else
echo "File Not Found"
fi

echo "____________________________________"
echo " Located Lines .... "
echo "____________________________________"

# Locate lines containing only printf but not fprintf
grep -e "printf" $fileName | grep -v "fprint"