0% found this document useful (0 votes)
3 views

BCA UNIX LAB

The document outlines a series of shell scripts for various tasks including counting characters in a string, checking leap years, determining even or odd numbers, calculating factorials, reversing strings, counting vowels, printing prime numbers, checking for palindromes, managing file permissions, compressing and decompressing files, implementing UNIX commands, checking file existence, and searching for patterns in files. Each lab includes clear instructions and code snippets for executing the tasks. The document serves as a practical guide for learning shell scripting in a Unix environment.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

BCA UNIX LAB

The document outlines a series of shell scripts for various tasks including counting characters in a string, checking leap years, determining even or odd numbers, calculating factorials, reversing strings, counting vowels, printing prime numbers, checking for palindromes, managing file permissions, compressing and decompressing files, implementing UNIX commands, checking file existence, and searching for patterns in files. Each lab includes clear instructions and code snippets for executing the tasks. The document serves as a practical guide for learning shell scripting in a Unix environment.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab 1 – Shell script to

count the number of


characters in a given
string
IV BCA Unix
Lab Programs
Part - A
IV BCA Unix
Lab Programs
Part - A
# Lab 1 – Shell script to
count the number of
characters in a given
string
# Lab 1 – Shell script to
count the number of
characters in a given
string
# Lab 1 – Shell script to count the number of characters in a given string
clear
echo "enter the string"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
echo "length of the $str is $len"

# Lab2 – Shell script to find whether the given year is leap year or not

# Lab2 – Shell script to


find whether the given
year is leap year or not
clear
echo "enter the year"
read y
if [ `expr $y % 4` -eq 0 ]; then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi

#Lab 3- Shell script to check whether given number is even or odd

clear
echo "enter a number"
read n
if [ `expr $n % 2` -eq 0 ]; then
echo " $n is even"
else
echo "$n is odd"
fi
#Lab 4 – Shell script to find the factorial of a given number

clear
echo "enter a number"
read n
if [ `expr $n % 2` -eq 0 ]; then
echo " $n is even"
else
echo "$n is odd"
fi
#Lab 4 – Shell script to find the factorial of a given number
clear
echo "enter a number"
read n
i=1
if [ $n -lt 0 ]; then
echo " you cannot find factorial for a negative number"
else
if [ $n -eq 0 ]; then
echo "factorial of zero is 1"
else
fact=1
while [ $i -le $n ]
do
fact=`expr $fact \* $i`

i=`expr $i + 1`
done
echo "the factorial of $n is $fact"
fi
fi

#Lab 5 – Shell script to reverse the given string


clear
echo "enter the string"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
while [ $len -gt 0 ]
do
ch=`echo $str | cut -c $len`
echo -n $ch
len=`expr $len - 1`
done

# Lab 6 - Shell script to count total number of vowels in a given string


# Lab 6 - Shell script to
count total number of
vowels in a given string
clear
echo "enter the string"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1;
while [ $i -le $len ]
do
ch=`echo $str | cut -c $i`
case $ch in
a | A) count=`expr $count + 1`;;
e | E) count=`expr $count + 1`;;
i | I) count=`expr $count + 1`;;
o | O) count=`expr $count + 1`;;
u | U) count=`expr $count + 1`;;
esac
i=`expr $i + 1`
done
echo "Total number of vowels in the given string are : $count "

#Lab 7 – Shell script to print prime numbers between the give n limits m and n

n
clear
echo " enter m and n value"
read m n
while [ $m -le $n ]
do
i=2
flag=0
while [ $i -le `expr $m / 2` ]
do
if [ `expr $m % $i` -eq 0 ]
the

flag=1
break
else
i=`expr $i + 1`
fi
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done

# Lab 8 – Shell script to check whether given string is palindrome or not


clear
echo "enter a string"
read str
#tempstr=$str
revstr=`echo $str | rev`
if [ $str = $revstr ]; then
echo " given string is palindrome"
else
echo "given string is not a palindrome"
fi

# Lab 9 – Shell script to


assign a file permission
to a given file using a)
symbolic mode b)
absolute mode
# Lab 9 – Shell script to
assign a file permission
to a given file using a)
symbolic mode b)
absolute mode
# Lab 9 – Shell script to
assign a file permission
to a given file using a)
symbolic mode b)
absolute mode
# Lab 9 – Shell script to
assign a file permission
to a given file using a)
symbolic mode b)
absolute mode
# Lab 9 – Shell script to assign a file permission to a given file using a) symbolic mode b)
absolute mode
clear
option=y
while [ $option = y ]
do
echo "1. Symbolic mode 2. Absolute mode"
echo "enter your choice"
read ch
case $ch in
1) clear
echo "changing the file permission in symbolic mode"
echo `pwd` contents are
ls -l
echo "enter the file name"
read fname
echo "enter the file type u/g/o"
read type
echo "enter one of the permission r/w/x"
read permission
#changing the mode with given input
chmod $type+$permission $fname
echo "after changing the permission"
ls -l
echo;;
2) clear
echo "changing the permission is absolute mode"
echo `pwd` contents are
ls -l
echo "enter the filename"
read fname
echo "enter the permission"
read permission
chmod $permission $fname
echo "after changind the file permission"
ls -l
echo;;

# Lab 10 - Shell script to compression and decompression of files using gzip and gunzip
clear
echo "enter the file name"
read fname
echo "size of $file name before compression is"
wc -c $fname
gzip $fname
echo "size of file name after compression"
wc -c $fname.gz
#unzip the file
gunzip $fname.gz
echo "after decompression $fname"
wc -c $fname
echo $fname

# Lab 11 - Menu driven


shell script to implement
the following UNIX
commands:
a) tail b) cmp
c) uniq d) rm -r
# Lab 11 - Menu driven shell script to implement the following UNIX commands:a) tail
b) cmp c) uniq d) rm –r
clear
opt=y
while [ $opt = y ]
do
clear
echo "----------Menu driven program --------"
echo
echo
echo "1. Usage of tail command "
echo
echo "2. Usage of cmp command"
echo
echo "3. Usage of uniq command"
echo
echo "4. Usage of rm -r command"
echo
echo "enter your choice"
read ch
case $ch in

echo "================================"
echo "uniq eliminates adjacent duplicate lines"
echo "======================================="
echo
echo unique lines of temp1
echo
echo "------------------------"
cat temp1 | sort | uniq;;
4) clear
echo "================================"
echo " rm -r deletes the directory along wih contents"
echo "============================================="
echo
echo "enter the subdirectory name"
read subdir
mkdir $subdir
cd $subdir
touch file1 file2
echo current directory is `pwd`
cd ..
if rm -r $subdir
then
echo $subdir removed succesffully
fi;;
*) echo "invalid choice"
esac
echo "do you like to continue ?"
read opt
done
echo
tail -$n temp;;
#Lab 12 - To check
whether the file exists or
not if it exists give
details of its attributes
like
access permission, size
and etc
#Lab 12 - To check whether the file exists or not if it exists give details of its attributes like
access permission, size and etc.
clear
echo "enter the file name"
read fname
if [ -e $fname ]
then
echo "$fname exists and its attributes are "
ls -l $fname
else
echo "$fname does not exist"
fi
esac
echo "do you want to continue y/n "
read option
done

#Lab 13 - Shell script to


search a given pattern
in a list of files of current
directory using
grep and fgrep
command
#Lab 13 - Shell script to search a given pattern in a list of files of current directory using
grep and fgrep command
clear
echo "enter the file name"
read fname1
echo "enter the contents of $fname1 and press ctrl+d"
cat >$fname1
echo "enter the file name 2"
read fname2
echo "enter the contents of $fname2 and press ctrl+w"
cat >$fname2
echo "enter the pattern"
read ptrn
#echo $ptrn $fname1 $fname2
grep -i $ptrn $fname1 $fname2
fgrep -i $ptrn $fname1 $fname2

You might also like