1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/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" |
Shell Math
Linux / UNIX Shell Scripts involving maths, academic and sys admin stuff
Script to display sum of two number and to do calculations such as +, -, / etc
in Categories Shell Math last updated April 15, 20081 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/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, 20081 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #!/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 Convert Fahrenheit to Celsius Temperature ( Celsius to Fahrenheit Temperature )
in Categories Decision Making, Shell Math last updated February 3, 2009Explains how to write temperature conversion formulas shell script to convert Fahrenheit to Celsius or vise versa.
Shell Script to read the base and height of a traingle and find its area
in Categories Shell Math last updated April 3, 2008Calculating the area of a triangle is an elementary problem encountered often in many different situations. The best known, and simplest formula is
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # 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" |