0% found this document useful (0 votes)
14 views8 pages

Shell Scripts for Common Calculations

The document contains multiple shell scripts that perform various tasks, including calculating the area of a circle, checking for Armstrong numbers, counting lines in a file, determining even or odd numbers, calculating factorials, finding the greatest of three integers, generating Fibonacci series, calculating gross salary, displaying messages based on time, and more. Each script prompts the user for input and executes specific operations based on that input. The scripts cover a wide range of functionalities, from basic arithmetic to string manipulation and file operations.

Uploaded by

Modrc Lu
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)
14 views8 pages

Shell Scripts for Common Calculations

The document contains multiple shell scripts that perform various tasks, including calculating the area of a circle, checking for Armstrong numbers, counting lines in a file, determining even or odd numbers, calculating factorials, finding the greatest of three integers, generating Fibonacci series, calculating gross salary, displaying messages based on time, and more. Each script prompts the user for input and executes specific operations based on that input. The scripts cover a wide range of functionalities, from basic arithmetic to string manipulation and file operations.

Uploaded by

Modrc Lu
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

Area of circle.

sh
echo "Enter the radius:"
read r
echo "area of the circle is"
echo "3.14*$r*$r"|bc

[Link]
echo "Enter a number:"
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo It is an armstrong number.
else
echo It is not an armstrong number.
Fi

Count of [Link]
echo Enter the filename
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo Number of characters in $file is $c
echo Number of words in $file is $w
echo Number of lines in $file is $l
Even or [Link]
echo "Enter a number:"
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

[Link]
echo "Enter a number"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num))
num=$((num - 1))
done
echo $fact

Greatest [Link]
echo "Enter three Integers:"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is Greatest"
elif [ $b -gt $c -a $b -gt $a ]
then
echo "$b is Greatest"
else
echo "$c is Greatest!"
fi
[Link]
echo "how many numbers do you want of fibonacci series?"
read total
x=0
y=1
i=2
echo"Fibonacci series up to $total terms ::"
echo $x
echo $y
while [ $i -lt $total ]
do
i=`expr $i + 1`
z=`expr $x + $y`
echo $z
x=$y
y=$z
Done

Grosss [Link]
echo enter the basic salary:
read bsal
gsal=$((bsal+((bsal/100)*40)+(bsal/100)*20))
echo The gross salary : $gsal

Message [Link]
check=`date +%H`
echo $check
if [ $check -ge 06 -a $check -le 12 ]
then
echo "Good morning"
elif [ $check -ge 12 -a $check -le 16 ]
then
echo "Good afternoon"
else
echo "Good evening"
fi
[Link]

echo Enter three integers with space between


read a b c
sum=`expr $a + $b + $c`
mean=`expr $sum / 3`
aa=$((($a - $mean) * ($a - $mean)))
bb=$((($b - $mean) * ($b - $mean)))
cc=$((($c - $mean) * ($c - $mean)))
sd=$( echo "sqrt(( $aa + $bb + $cc ) / 3)"| bc -l )
echo sum= $sum
echo mean= $mean
echo Sd= $sd

[Link]
echo Enter the string
read s
echo $s>temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo "It is palindrome"
else
echo "it is not palindrome"
Fi

[Link]
echo Enter the filename
read filename
echo "Contents of $filename before converting to uppercase"
cat $filename
echo "Contents of $filename after converting to uppercase"
tr '[a-z]' '[A-Z]'<$filename
Power [Link]
echo "Input number"
read no
echo "Input power"
read power
counter=0
ans=1
while [ $power -ne $counter ]
do
ans=`expr $ans \* $no`
counter=`expr $counter + 1`
done
echo "$no power of $power is $ans"

[Link]
echo "Enter a line of text:"
read string
numCount=$(echo $string | grep -o "[0-9]" | wc --lines)
vowCount=$(echo $string | grep -o -i "[aeiou]" | wc --lines)
consCount=$(echo $string | grep -o -i "[bcdfghjklmnpqrstvwxyz]" | wc
--lines)
echo "The given string has $vowCount vowels, $consCount
consonants and $numCount numbers in it."

Sum of [Link]
echo "Enter a Number:"
read n
temp=$n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))
done
echo Sum is $sum
Prime [Link]
read -p "How many prime numbers ?: " num
c=0
k=0
n=2
echo 1
numero=$[$num-1]
while [ $k -ne $num ]; do
for i in `seq 1 $n`;do
r=$[ $n % $i ]
if [ $r -eq 0 ]; then
c=$[ $c + 1]
fi
done
if [ $c -eq 2 ]; then
echo "$i"
k=$[ $k + 1 ]
fi
n=$[ $n + 1 ]
c=0
done

[Link]
echo "Enter a Number:"
read a
rev=0
sd=0
or=$a
while [ $a -gt 0 ]
do
sd=`expr $a % 10`
temp=`expr $rev \* 10`
rev=`expr $temp + $sd`
a=`expr $a / 10`
done
echo Reverse of $or is $rev
[Link]
sum=0
i="y"
echo "Enter first number :"
read n1
echo "Enter second number :"
read n2
while [ $i = "y" ]
do
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "[Link]"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sub=`expr $n1 - $n2`
echo "Sub = "$sub;;
3)mul=`expr $n1 \* $n2`
echo "Mul = "$mul;;
4)div=`echo $n1 / $n2 | bc -l`
echo "Div = "$div;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
copy,rnme, [Link]

#!/bin/bash echo "$f1 is renamed as $f2";;


i="y" 6)echo "Enter any filenm to be
while [ $i = "y" ] delete"
do read f1
echo "[Link] current dir" rm "$f1"
echo "[Link] the dir" echo "$f1 is deleted";;
echo "[Link] a dir" 7)echo "Enter any file to be editing
echo "[Link] a file" "
echo "[Link] file" read f1
echo "[Link] file" vi "$f1";;
echo "[Link] file" 8)echo "Have a nice time"
echo "[Link]" exit;;
echo "Enter your choice" *)echo "Invalid choice entered";;
read ch esac
case "$ch" in echo "Do u want to continue ? "
1)echo "Current Dir is : " read i
pwd ;; if [ $i != "y" ]
2)echo "Directories are" then
ls ;; exit
3)echo "Enter dir name to create" fi
read d done
mkdir "$d"
echo "$d Dir is created" ;;
4)echo "Enter filename from copy"
read f1
echo "Enter filenm2 to be copied"
read f2
#cat $f1 > $f2
cp "$f1" "$f2"
echo "$f2 is copied from $f1";;
5)echo "Enter file name to
rename"
read f1
echo "Enter new name of file"
read f2
mv "$f1" "$f2"

You might also like