Professional Documents
Culture Documents
UNIX - PartA Lab
UNIX - PartA Lab
UNIX - PartA Lab
PART A
1. Write a shell script to print all the prime numbers between m to n (m<n).
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
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
cat> f2
Apple
Grapes
Banana
Pineapple
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 ;;
8. Write awk script to find number of words, characters and lines in a file.
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