0% found this document useful (0 votes)
22 views3 pages

Shell

The document is a Bash script that performs various tasks including creating directories and files, counting lines and words in a file, calculating vowels in a text line, summing and averaging integers, computing simple and compound interest, and more. It also includes functionalities for determining student grades, generating Fibonacci series, finding roots of quadratic equations, and checking if numbers are positive or negative. Additional features include displaying digits in odd positions, checking for prime numbers, calculating the area of a triangle, finding the smallest digit in a number, and converting Fahrenheit to Celsius.

Uploaded by

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

Shell

The document is a Bash script that performs various tasks including creating directories and files, counting lines and words in a file, calculating vowels in a text line, summing and averaging integers, computing simple and compound interest, and more. It also includes functionalities for determining student grades, generating Fibonacci series, finding roots of quadratic equations, and checking if numbers are positive or negative. Additional features include displaying digits in odd positions, checking for prime numbers, calculating the area of a triangle, finding the smallest digit in a number, and converting Fahrenheit to Celsius.

Uploaded by

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

#!

/bin/bash

# 1(b) Create directory and files, list them


mkdir -p mydir
cd mydir || exit
touch file1.txt file2.txt
ls
cd ..

# 2(a) Count lines, words, characters


echo "Enter filename for wc:"
read file
wc "$file"

# 4(b) Count vowels in input


echo "Enter a line of text:"
read line
count=$(echo "$line" | grep -o -i '[aeiou]' | wc -l)
echo "Number of vowels: $count"

# 5(b) Sum and average of four integers


echo "Enter four integers:"
read a b c d
sum=$((a + b + c + d))
avg=$((sum / 4))
echo "Sum: $sum"
echo "Average: $avg"

# 6(b) Simple and compound interest


echo "Enter Principal, Rate and Time:"
read p r t
si=$(( (p * r * t) / 100 ))
ci=$(echo "scale=2; $p * (1 + $r / 100)^$t - $p" | bc)
echo "Simple Interest: $si"
echo "Compound Interest: $ci"

# 7(b) Area and circumference of circle


echo "Enter radius:"
read r
area=$(echo "scale=2; 3.14 * $r * $r" | bc)
circumference=$(echo "scale=2; 2 * 3.14 * $r" | bc)
echo "Area: $area"
echo "Circumference: $circumference"

# 9(a) Student grades


echo "Enter mark:"
read mark
if [ $mark -ge 90 ]; then
echo "Grade: A+"
elif [ $mark -ge 80 ]; then
echo "Grade: A"
elif [ $mark -ge 70 ]; then
echo "Grade: B"
elif [ $mark -ge 60 ]; then
echo "Grade: C"
else
echo "Grade: F"
fi
# 10(b) Fibonacci series
echo "Enter number of terms:"
read n
a=0
b=1
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo

# 11(b) Roots of quadratic equation


echo "Enter a, b, c:"
read a b c
d=$((b*b - 4*a*c))
if [ $d -lt 0 ]; then
echo "No real roots"
else
r1=$(echo "scale=2; (-$b + sqrt($d)) / (2*$a)" | bc)
r2=$(echo "scale=2; (-$b - sqrt($d)) / (2*$a)" | bc)
echo "Roots: $r1 and $r2"
fi

# 12(b) Check if number is positive, negative or zero


echo "Enter a number:"
read n
if [ $n -gt 0 ]; then
echo "Positive"
elif [ $n -lt 0 ]; then
echo "Negative"
else
echo "Zero"
fi

# 13(a) Display digits in odd positions


echo "Enter a number:"
read num
len=${#num}
for (( i=0; i<$len; i++ ))
do
if (( $((i % 2)) == 0 )); then
echo -n "${num:$i:1} "
fi
done
echo

# 14(a) Sum using function


sum() {
result=$(( $1 + $2 ))
echo "Sum: $result"
}
echo "Enter two numbers:"
read a b
sum $a $b

# 15(b) Check if number is prime


echo "Enter number:"
read num
is_prime=1
for (( i=2; i*i<=num; i++ ))
do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo "Prime"
else
echo "Not Prime"
fi

# 19(b) Area of triangle


echo "Enter base and height:"
read base height
area=$(echo "scale=2; 0.5 * $base * $height" | bc)
echo "Area: $area"

# 20(a) Smallest digit in a number


echo "Enter number:"
read num
min=9
for (( i=0; i<${#num}; i++ ))
do
digit=${num:$i:1}
if (( digit < min )); then
min=$digit
fi
done
echo "Smallest digit: $min"

# 23(a) Smallest of two using function


smallest() {
if [ $1 -lt $2 ]; then
echo "$1 is smaller"
else
echo "$2 is smaller"
fi
}
echo "Enter two numbers:"
read a b
smallest $a $b

# 26(b) Fahrenheit to Celsius


echo "Enter temperature in Fahrenheit:"
read f
c=$(echo "scale=2; ($f - 32) * 5 / 9" | bc)
echo "Celsius: $c"

You might also like