0% found this document useful (0 votes)
16 views6 pages

Aat 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Aat 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.Take input of three numbers from the user and find the largest and smallest among three.

echo "Enter three numbers"


read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo $a is larger
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo $b is larger
else
echo $c is larger
fi
if [ $a -lt $b ] && [ $a -lt $c ]
then
echo $a is small
elif [ $b -lt $a ] && [ $b -lt $c ]
then
echo $b is small
else
echo $c is small
fi

2.Read a string and check if it is empty. Display message appropriately.


echo "Enter the string"
read a
if [ -z $a ]
then
echo "String is empty"
else
echo " String is not empty"
fi

3.Read a filename as an input from command line and check the type of the file (regular,
directory, device, link).
if [ $# -eq 0 ]
then
echo "Provide the filename in command line"
exit 1
fi
if [ -e $1 ]
then
if [ -f $1 ]
then
echo "$1 is a regular file"
elif [ -d $1 ]
then
echo "$1 is a directory"
elif [ -b $1 ]
then
echo "$1 is a block device file"
elif [ -c $1 ]
then
echo "$1 is a character device file"
elif [ -h $1 ]
then
echo "$1 is a symbolic link"
else
echo "$1 is of unknown type"
fi
else
echo "$1 does not exists"
fi

4.Read the principle amount, time and rate of interest. Calculate thesimple interest.
echo "Enter the principle Amount:"
read p
echo "Enter the Time:"
read t
echo "Enter the Rate of Interest:"
read r
SI=$(echo "scale=2; $p * $t * $r / 100"|bc)
echo "SIMPLE INTEREST IS: $SI"

5.Read a directory name (absolute path name) and then display only the properties of regular
files it contains.

echo "Enter the directory name(absolute path):"


read directory
if [ -d "$directory" ]
then
echo "Properties of regular files in the directory are:"
ls -l $directory | grep '^-r'
else
echo "$directory does not exists"
fi

6.Read a file name from the command line as absolute path name and check if it has execute
permission for all 3 types of users.
if [ $# -eq -0 ]
then
echo "Provide the file name in command line"
exit 1
fi
file1=$1
if [ -f "$file1" ]
then
if [ -x "$file1" ]
then
echo "The file "$file1" has execute permission for all 3 types of users"
else
echo "The file "$file1" does not have execute permission for all 3 types of users"
fi
else
echo "The file "$file1" does not exists or is not a regular file"
fi

7.Read two strings from the command line and check if they are same or not.
if [ $# -eq -0 ]
then
echo "Please Provide both the strings in command line"
exit 1
fi
echo "The First String is : "$1
echo "The Second String is : "$2
if [ "$1" = "$2" ]
then
echo "The Given Strings are SAME"
else
echo "The Given Strings are DIFFERENT"
fi

8.Write a shell script to read a file name from the command line, delete all the blank lines in
the file and display all the contents of the file.
if [ $# -eq -0 ]
then
echo "Please Provide the filename in command line"
exit 1
fi
file1=$1
echo "Before deleting all the blank lines:"
cat $file1

sed -i '/^$/d' $file1

echo "After deleting all the blank lines:"


cat $file1

9.Write a shell script to read a file name, search pattern, replace pattern from the command
line and execute accordingly.
if [ $# -eq -0 ]

then
echo "Please Provide the patterns in command line"
exit 1
fi
file=$1
search_pattern=$2
replace_pattern=$3
echo "File name is: "$file
echo "Search Pattern is: "$search_pattern
echo "Replace Pattern is: "$replace_pattern
echo "Before Replacing:"
cat $file
sed -i "s/${search_pattern}/${replace_pattern}/g" $file
echo "After Replacing:"
cat $file

10. Read the user choice and compute basic arithmetic operations based on the choice.
echo "Enter the First Number:"
read num1
echo "Enter the Second Number:"
read num2
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice(1/2/3/4):"
read choice

case $choice in
1)echo "$num1 + $num2 = $(echo "scale=2; ($num1 + $num2)" | bc)";;
2)echo "$num1 - $num2 = $(echo "scale=2; ($num1 - $num2)" | bc)";;
3)echo "$num1 * $num2 = $(echo "scale=2; ($num1 * $num2)" | bc)";;
4)if [ $(echo "$num2==0"|bc) -eq 1 ]
then
echo "Division by Zero Error"
else
echo "$num1 / $num2 = $(echo "scale=2; ($num1 / $num2)" | bc)"
fi;;
*)echo "Invalid choice"
esac

You might also like