Tuesday, May 11, 2010

Ubuntu a script calculator in terminal

In ubuntu a command line calculator as follows.

Please open a text file namely 'calcu' in terminal and copy & paste the following lines.

sudo gedit calcu

#!/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.
# -------------------------------------------------------------------------

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

save and exit.

Type the following line in terminal

sudo chmod +x calcu */the calcu is script file name,

1. plus
./calcu 23 + 23 =46

2.multiply
./calcu 100 x 2 = 200

3.divide
./calcu 100 / 2 = 50

4.minus
./calcu 100 - 2 = 98


0 comments: