Shell Script To Simulate a Simple Calculator

by Vivek Gite [Last updated: October 20, 2008]

in Academic, Shell Math

#!/bin/bash
# Shell Program to simulate a simple calculator
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
a=$1
op="$2"
b=$3
 
if [ $# -lt 3 ]
then
	echo "$0 num1  opr num2"
	echo "opr can be +, -, / , x"
	exit 1
fi
 
case "$op" in
	+) echo $(( $a + $b ));;
	-) echo $(( $a - $b ));;
	/) echo $(( $a / $b ));;
	x) echo $(( $a * $b ));;
	*) echo "Error ";;
esac
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 1 comment… read it below or add one }

Andrew Johnstone 10.28.08 at 1:06 am

Whats wrong with “bc - An arbitrary precision calculator language “???

echo “((5*2)/2)%2″ | bc

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , ,

Previous post: Shell Script To Sort Numbers in File In Ascending Order

Next post: Shell Script To Count Vowels, Blank Spaces, Characters, Number of line and Symbols