Aat 2
Aat 2
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.
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
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