Help with calculator code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with calculator code
# 1  
Old 02-01-2010
Help with calculator code

Hi Guys,

I found this code in net.. it is working fine..
But can anybody explain me the sed statement used in the code..

Code:
    echo "Enter the expression:\c"
read express
eval echo "$express"|sed 's/^/'$precision' \
/'|bc -l|\
    sed -n '1,${
        /syntax/!{
        }
        /syntax/{
            a\
                Possible causes:\
                1. Error in expression\
                2. Variable not enclosed in (), supplied -ve value\
                3. Exponents given as non-integers
        }
        s/\([ ]*\)\([^ ]*\)/\2/p
    }'

Thanks For your help in advance..

Regards,
Magesh

---------- Post updated 02-02-10 at 12:40 AM ---------- Previous update was 02-01-10 at 08:35 PM ----------

Guys come on man.. i was counting on you people

Last edited by mac4rfree; 02-01-2010 at 02:36 PM..
# 2  
Old 02-01-2010
Hi,

I'm learning sed with:
Sed - An Introduction and Tutorial

So:


Ask for expresion:
Code:
echo "Enter the expression:\c"
read express

Appended some precision variable (must have been definited before) and run through bc (calculator)
Code:
eval echo "$express"|sed 's/^/'$precision' \
/'|bc -l|\


Don't print anything if not request by /p argument (-n switch) on every line (1,$).

Code:
    sed -n '1,${

If no "syntax" in line was found - don't print nothing.
Code:
        /syntax/!{
        }

For lines that do contain "syntax" word:
Code:
        /syntax/{

... append more explanation:
Code:
            a\
                Possible causes:\
                1. Error in expression\
                2. Variable not enclosed in (), supplied -ve value\
                3. Exponents given as non-integers
        }

Finally, print the word first word (string/digits) of lines which are starting with whitespaces. I guess this is a format of bc final result.

Code:
 
        s/\([ ]*\)\([^ ]*\)/\2/p
    }'


Not bad for an hour of reading, I guess. Please correct me if I'm wrong - I'm just learning.

Last edited by dpc.ucore.info; 02-01-2010 at 06:43 PM.. Reason: fixes
# 3  
Old 02-08-2010
Thanks man.. really appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Calculator

Hello, I have to make a calculator in shell script. But I get this error. Can someone help me please? c.sh: 3: c.sh: i: not found That's my code. ========================================================================== #Calculator i = "yes" while do echo What operation... (2 Replies)
Discussion started by: KJN
2 Replies

2. Shell Programming and Scripting

CSH Calculator Script

Using the C Shell, I'm building a script that will compute simple mathematical computations (addition, subtraction, multiplication, division). The user will enter two integers (operands) on the command line separated by the operation (operator) they wish to perform. Example of the command line... (7 Replies)
Discussion started by: ksmarine1980
7 Replies

3. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

4. Shell Programming and Scripting

chmod calculator script

so just spit ballin here, i was wondering if anybody knew how to make a chmod calculator script. basically go to this website http://mistupid.com/internet/chmod.htm i would like something like this that i can use in a terminal tho. so like i run the scrip and it ask for owner what... (1 Reply)
Discussion started by: hookitup
1 Replies

5. Homework & Coursework Questions

Problem with calculator script

I'm having some trouble implementing a basic calculator using command line options. The script is supposed to take (multiple) arguments -a,-d,-m,-s for addition, multiplication, division, and subtraction. I'm pretty sure I know how to parse through the options with getopt(), but I have no idea... (17 Replies)
Discussion started by: zkapopou
17 Replies

6. Shell Programming and Scripting

Bash Calculator issue

Hello, I'm relatively new to using bc so I could use some help. In this script im working on I want to have the bc function to calculate float numbers for imagemagicks convert charcoal. Below is what I'm talking about. There are no syntax errors but when it outputs the users frames for example 0-10... (2 Replies)
Discussion started by: jsells20
2 Replies

7. UNIX for Dummies Questions & Answers

calculator

hi, im new to the unix system and scripting and was wondering if anyone could help me with this problem iv been havin... i want the system to: 1. ask me for a number 2. ask me for a command to use on that number (* + - /) 3. ask me for another number 4. then ask me for another command, if the... (2 Replies)
Discussion started by: jdougy
2 Replies

8. Shell Programming and Scripting

Calculator

I am pretty new to the Unix word, and have created a working calculator script. I have one problem. It doesn't use any decimals, it rounds off to the nearest whole number. 1 #!/bin/ksh 2 while true; do 3 echo -n "Enter the first integer: "; read IN1 4 test... (2 Replies)
Discussion started by: ironhead3fan
2 Replies

9. Shell Programming and Scripting

calculator program..

Hey can anyone tell me the korn script code to implement an interactive integer calculator using the shell's built in arithemetic expression evaluation (2 Replies)
Discussion started by: sahithi_khushi
2 Replies
Login or Register to Ask a Question