You are on page 1of 5

Operating System Lab

Assignment-2

1.Write a shell script to input a file name and display its rights.
echo -n "Enter file name : "
read file

[ -w $file ] && W="Write = yes" || W="Write = No"

[ -x $file ] && X="Execute = yes" || X="Execute = No"

[ -r $file ] && R="Read = yes" || R="Read = No"

echo "$file permissions"


echo "$W"
echo "$R"
echo "$X"
2. Write a shell script to count number of vowels in file ignoring the
case.
#!/bin/bash
clear
echo
read str
len=$(expr length $str)
count=0
while [ $len -gt 0 ]
do
ch=$(echo $str | cut -c $len)
case $ch in
[aeiouAEIOU] )
count=$(($count + 1))
echo $ch
len=$(( $len - 1 ))
done
echo $count
3. Write a shell script to find out a given string is palindrome or not.
len=0
i=1
tag=0
echo -n “Enter a String: ”
read str
len=`echo $str | wc -c`
len=`expr $len – 1`
halfLen=`expr $len / 2`
while [ $i -le $halfLen ]
do
c1=`echo $str|cut -c$i`
c2=`echo $str|cut -c$len`
if [ $c1 != $c2 ] ; then
i=$halfLen
tag=1
fi
i=`expr $i + 1`
len=`expr $len – 1`
done
if [ $tag -eq 0 ]
then
echo “String is Palindrome”
else
echo “String is not Palindrome”
4. Write a shell script which deletes all fines containing the word
Unix in the files supplied in file as arguments.

for file in $*
do
grep –iv “Unix” $file | tee 1> /dev/null
done
echo “ lines containing given word are deleted”

5. Write a shell script to calculate simple interest.


echo " Enter the principle value: "
read p
echo " Enter the rate of interest:"
read r
echo " Enter the time period:"
read t
s=`expr $p \* $t \* $r / 100`
echo " The simple interest is "
echo $s
6. Write a script that counts the number of files ending in .sh in the
current directory.
exts=( *.sh ); printf "There are ${#exts[@]}" extensions;
7. Write a shell script to display date in different formats along with
time.

#!/bin/bash
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt"

8. Write a script to print specific lines of a file. Here starting line no


and end line no is given.
sed -n '20,25p' lines.txt
(where 20 is starting line number and 25 is last line number)
9. Write a shell script to print the reverse of a given integer.
#!/bin/bash
clear
read -p "Enter a number: " num
echo $num | rev

10.Create an array of 10 numbers. Write a shell program to find the .


largest of the ten numbers
i=1
max=0

echo "Enter Numbers"


while [ $i -le 10 ]
do
read num
if [ $i -eq 1 ]
then
max=$num
else
if [ $num -gt $max ]
then
max=$num
fi
fi
i=$((i + 1))
done

echo $max

You might also like