Shell script to read 5 digit number and calculate the sum of digit

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Shell script to read 5 digit number and calculate the sum of digit
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter numnber : "
read n
 
# find out length of string using wc -c command
len=$(echo $n | wc -c)
 
# remove \n i.e. new line character
len=$(( $len - 1 ))
 
# use loop to go throug all digit one by one and calculate sum of digit on fly
for (( i=1; i <= $len; i++ ))
do
   sum=$(( $sum + $(echo $n | cut -c $i) ))
done
echo "Sum of $n is $sum"

Shell Script Calulate Area and Circumference of Circle

in Categories Shell Math last updated April 4, 2008

A circle is one of the simple shapes of Euclidean geometry. It is the locus of all points in a plane at a constant distance, called the radius, from a fixed point, called the center. Through any three points not on the same line, there passes one and only one circle.

A diameter is a chord passing through the center. The length of a diameter is twice the radius. A diameter is the largest chord in a circle.

Circles are simple closed curves which divide the plane into an interior and an exterior. The circumference of a circle is the perimeter of the circle, and the interior of the circle is called a disk.

Sample Shell Script To Calculate Area and Circumference of Circle

#!/bin/bash
# Shell script to calulate area and Circumference of circle. 
# It take radius of a circle as input.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# Lesson on Circumference of a Circle : 
#  Visit http://www.mathgoodies.com/lessons/vol2/circumference.html
echo -n "Enter the radius of a circle : "
read r
 
# use formula to get it
area=$(echo "scale=2;3.14 * ($r * $r)" | bc)
 
# use formula to get it
d=$(echo "scale=2;2 * $r"|bc)
circumference=$(echo "scale=2;3.14 * $d"| bc)
 
echo "Area of circle is $area"
echo "Circumference of circle is $circumference"

Shell Script to read price of an article and calculate discount

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Script to read price of an article. If the price is less than 100
# then display "No discount" else give a discount of 10%. Display
# the price of article after discount
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter price of an article : "
read price
 
if [ $price -lt 100 ]
then
   echo "No discount "
   d=0 # 0 means no discount
else
   echo "10% discount "	
   d=$(( $price  * 10 / 100 )) # 10% discount
fi
# how much user need to pay? after discount
pay=$(( $price - $d ))
echo "You need to pay INR. $pay"

Script to display sum of two number and to do calculations such as +, -, / etc

in Categories Shell Math last updated April 15, 2008
#!/bin/bash
# Script to display sum of two number and to do calculations such as +, -, / etc
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo "**** My calculator ****"
echo "M A I N - M E N U"
echo "1. Multiplication"
echo "2. Subtraction"
echo "3. Remainder"
echo "4. Divide"
echo -n "Please select your choice (1-4) : "
read choice
 
echo -n "Enter your first number : "
read n1
echo -n "Enter your second number : "
read n2
 
if [ $choice -eq 1 ]
then
	answer="$n1 x $n2 = $(( $n1 * $n2 ))"
elif [ $choice -eq 2 ]
then
	answer="$n1 - $n2 = $(( $n1 - $n2 ))"
elif [ $choice -eq 3 ]
then
	answer="$n1 % $n2 = $(( $n1 % $n2 ))"
elif [ $choice -eq 4 ]
then
	answer="$n1 / $n2 = $(( $n1 / $n2 ))"
else
	echo "Sorry please select number between 1-4 only"
	exit 1
fi
echo $answer

Shell script to read 3 numbers and find the greaters of the three

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Shell script to read 3 numbers and find the greaters of the three
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo -n "Please enter three numbers (separate number by space) : "
read a b c
# compare a with b and c. Note -a is logical and operator
if [ $a -gt $b -a $a -gt $c ]
then
   big=$a
elif  [ $b -gt $a -a $b -gt $c ] # compare b with a and c
then
   big=$b
elif  [ $c -gt $a -a $c -gt $b ] # compare c with a and b
then
   big=$c
elif [ $a -eq $b -a $a -eq $c -a $b -eq $c -a $c -eq $b ] # see if all of them are equal or not
then
   big="All three numbers are same (equal)"
else # something must be wrong if we are here, like one of number is character such as 'A'
   big="Can not guess greaters of three numbers"
fi
# display result 
echo "Result : $big"

Shell Script to read the base and height of a traingle and find its area

in Categories Shell Math last updated April 3, 2008

Calculating the area of a triangle is an elementary problem encountered often in many different situations. The best known, and simplest formula is

Computing the area of a triangle

Where S is area, b is the length of the base of the triangle, and h is the height or altitude of the triangle. The term ‘base’ denotes any side, and ‘height’ denotes the length of a perpendicular from the point opposite the side onto the side itself.

# Shell program/script to read the base and height of a traingle and find its area
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html
 
# Area=(1/2) x Base x Height
echo -n "Enter base of a triangle : "
read b
 
echo -n "Enter height of a triangle : "
read h
# calculate it and display back
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
echo "Area of a triangle is $area"

Shell Script to display current date, calendar, and the number of user logged

in Categories Academic last updated April 3, 2008

This script use the concept of command substitution. It allows allows the output of a command to replace the command name. There are two forms:

$(command)

or

`command`

Bash shell performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

For example you can execute date command to display todays date and then set a variable called NOW:
NOW=$(date)
or
NOW=`date`
To display back variable value simply run echo command:
echo $NOW

Sample Shell Script

#!/bin/bash
# Shell script to display current date, calendar, and the number of
# user logged.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo "Today is $(date)"
echo ""
 
echo "Calendar :"
cal
 
echo "Number of users currently logged : $(who | wc -l)"

Shell Script to ask user his / her name and then display it with a G’Day/Good morning message

in Categories Academic last updated April 3, 2008

This script greets user with a message. Then it displays prompt asking user her name. Once name entered it will display Gooday message using echo command.

read statement will read one line from the user (keyboard) or from file and store it to a variable called yourname.

#!/bin/bash
# Shell Script to ask user his/her name and then display it with a
# GoodDay/Good morning message
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo "Hi! I'm $(hostname) computer!!"
echo -n "May I know your name please ? "
read yourname
echo "Goo Day $yourname"