UNIX - PartA Lab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

UNIX AND SHELL PROGRAMMING LAB

PART A

1. Write a shell script to print all the prime numbers between m to n (m<n).

echo "Enter the value of m"


read m
echo "Enter the value of n"
read n
echo "Prime numbers in the range $m and $n are:"
echo
while [ $m -le $n ]
do
i=2
flag=1
while [ $i -lt $m ]
do
if [ `expr $m % $i` -eq 0 ]
then
flag=0
break
else
i=`expr $i + 1`
fi
done
if [ $flag -eq 1 ]
then
echo $m
fi
m=`expr $m + 1`
done
2. Write a shell script to reverse a given number and check whether it is
a palindrome.

echo -e "\n Enter a number : \n "


read num
original=$num
rev=0
while [ $num -gt 0 ]
do
rem=`expr $num % 10` # get Remainder
rev=`expr $rev \* 10 + $rem`
num=`expr $num / 10` # get next digit
done
echo Reverse is: $rev
if [ $original -eq $rev ]
then
echo $original is a
palindrome else
echo $original is NOT a
palindrome fi
3. Write a shell script to implement 10 unix commands using case .

clear
opt=y
while [ $opt=y ]
do
echo -e "\n Menu \n"
echo -e "\n 1. List of files"
echo -e "\n 2. Current Date"
echo -e "\n 3. Process Status"
echo -e "\n 4. Logged in Users"
echo -e "\n 5. Present working directory"
echo -e "\n 6. Disk space available"
echo -e "\n 7. System Details"
echo -e "\n 8. Usage of tail command"
echo -e "\n 9. Usage of uniq command"
echo -e "\n 10. Quit"
echo -e "\n Enter your option : \c"
read ch
case $ch in
1) ls -l ;;
2) date ;;
3) ps ;;
4) who ;;
5) pwd ;;
6) df -h ;;
7) uname -a;;
8) echo "tail displays lines from bottom of the
file" echo
echo "Enter the number of lines"
read n
echo last $n lines of the file f1 are:
tail -$n f1;;
9) echo "uniq eliminates duplicate
lines" echo
echo Unique lines of f1 are:
echo
cat f1 | sort | uniq ;;
10) exit ;;
*) echo "Invalid Choice"
esac
echo "Do you want to continue?"
read opt
done
4. Write a Shell script that displays list of all the files in the current directory
to which the user has read, write and execute permissions?

clear
echo "To display file types and their permission"
echo "Ordinary or regular files are:"
for file in *
do
if [ -f $file ]
then
echo $file
fi
done
echo "********************************"
echo "Directory files are:"
for file in *
do
if [ -d $file ]
then
echo $file
fi
done
echo "*********************************"
echo "Files with read persmision are:"
for file in *
do
if [ -f $file ]
then
if [ -r $file ]
then
echo $file
fi
fi
done
echo "**********************************"
echo "Files with write permission are:"
for file in *
do
if [ -f $file ]
then
if [ -w $file ]
then
echo $file
fi
fi
done
echo "********************************"
echo "Files with execute permission are:"
for file in *
do
if [ -f $file ]
then
if [ -x $file ]
then
echo $file
fi
fi
done

5. Write a shell script to:


i) copy file within current directory.
ii)copy file between two directories.

clear
echo "**********Creating and moving files***********"
echo
echo "Creating first directoy and storing 2
files" echo " "
echo
echo "Enter first directory name"
read dir1
mkdir $dir1
echo Directory $dir1 is created successfully
cd $dir1
touch f1 f2
echo $dir1 contains:
ls
echo
echo "Copying File f1 to File f3"
echo " "
cp f1 f3
echo $dir1 contains
ls
cd ..
echo
echo "Creating second dir"
echo " "
echo "Enter second direcory name"
read dir2
mkdir $dir2
echo Directory $dir2 is created successfully
cd $dir1
cp f1 f2 f3 /home/anumol/Desktop/pgms/$dir2 #to find the path use command pwd
echo Files f1 f2 f3 are successfully copied from $dir1 to $dir2
cd ..
cd $dir2
echo
echo After copying the files from $dir1, $dir2 now contains:
echo " "
ls
cd ..
#rm -r $dir1 $dir2 #Removes the newly created directories dir1 and dir2
6. Write a Shell script to create 2 data files and compare them to display
unique and common entries.

clear
echo "Enter the first file name"
read file1
echo "Enter the second file name"
read file2
echo "Using comm entries"
comm $file1 $file2
echo " "
echo "Using unique entries in first file"
cat $file1 | sort | uniq
echo " "
echo "Using unique entries in second file"
cat $file2 | sort | uniq
#Create two files in Interactive mode by using following command(f1 and
f2):

cat> f1
Apple
Banana
Banana
Orange

#Press Ctrl+ D once you finish entering the values

cat> f2
Apple
Grapes
Banana
Pineapple

#Press Ctrl+ D once you finish entering the values


7. Write a Menu driven shell script to perform the following
a. To count the number of vowels in a string.
b. To convert uppercase characters to lowercase and vice versa.
c. To accept a word and perform pattern matching in a given file.

clear
opt=y
while [ $opt=y ]
do
echo -e "\n Menu "
echo -e "\n 1. Count the number of Vowels"
echo -e "\n 2. Convert Upper case to lower case and vice versa"
echo -e "\n 3. To accept a word and perform pattern matching in a given
file"
echo -e "\n Enter your option : \c"
read ch
case $ch in
1)
echo "Enter a string to find the number of Vowels "
cat> str
len=`cat str | wc -c`
i=1
count=0
while [ $i -le $len ]
do
ch=`cat str | cut -c $i`
case $ch in
[aeiouAEIOU]) count=`expr $count + 1` ;;
esac
i=`expr $i + 1`
done
echo "Number of vowels in the given string is "$count ;;

2)
echo Enter the filename
read filename
echo Contents of $filename before converting to uppercase
echo
cat $filename
echo
echo Contents of $filename after converting to uppercase
echo
tr '[a-zA-Z]' '[A-Za-z]' < $filename
echo ;;

3)
echo Enter the filename
read filename
echo Contents of $filename is:
echo
cat $filename
echo Enter the name to be searched -Using grep command
read name
grep -n "$name" $filename ;;

*) echo "Invalid Choice"


esac
echo "Do you want to continue?"
read opt
done

8. Write awk script to find number of words, characters and lines in a file.

BEGIN{print "record \t characters \t words"}


#BODY section
{
len=length($0)
print(NR,":\t",len,":\t \t ",NF,":\t",$0)
words += NF
total_len += len
}
END{
print("\n Total number of Characters :" total_len)
print("\n Total number of Words :" words)
print("\n Total number of Lines :" FNR)
}
9. Create an emp file containing empname, empno, dept, designation.
a. Display empname and empno of any particular dept and the count of
employees.
b. Display empname and empno of employees who are not managers.

clear
echo "Enter a filename"
read file
echo "Employee details"
echo " "
echo "Ename Eno Dept Destignation"
echo " "
awk -F "|" '{print $1,$2,$3,$4}' $file
echo "*******************************"
echo "Query-1:-"
echo "Ename Eno Dept"
echo " "
awk -F "|" '$3 ~/acct/{print $1,$2,$3}' $file
awk -F "|" '$3 ~/acct/{print $1,$2,$3}' $file | wc -l
echo "*****************"
echo "Query-2:-"
echo "Ename Eno"
echo " "
awk -F "|" '$4 !~/manager/{print $1,$2}' $file

You might also like