CSH Calculator Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSH Calculator Script
# 1  
Old 11-13-2014
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 would be as follows: ./scriptname 5 minus 2

I have that part working perfectly. Where I'm running into a problem is the following:

1) If the any of the operands or operator are missing from the command line I want to print an error

2) If the user enters an invalid operator (not +, -, %, X) the script shall will print an error)

I've attached my script below. If you have any recommendations for the above two issues your help would be greatly appreciated. Thank you!

Code:
if ( $2 =~ "plus" ) then
        @ c = $1 + $3
        echo "The sum is: $c"

else if ( $2 =~ "minus" ) then
        @ c = $1 - $3
        echo "The difference is: $c"

else if ( $2 =~ "div" ) then
        @ c = $1 / $3
        echo "The quotient is: $c"

else if ( $2 =~ "times" ) then
        @ c = $1 * $3
        echo "The product is: $c"

else if ( ( $2 !~ "plus" || $2 !~ "minus" ) && ( $2 !~ "div" || $2 !~ "times" ) ) then
        echo "You have entered an invalid operation"

else
        echo "You are missing an operand or operator"
endif

# 2  
Old 11-13-2014
That last statement looks over-complicated to me I think:

Code:
if ( $2 == "string" ) then
...
else if ( $2 == "other" ) then
...
else
        echo "operator is invalid or missing"
endif

But this would be easier with a case I think:

Code:
switch ($2)
     case plus:
       ...
       breaksw
     case minus:
       ...
       breaksw
     ...
     default:
       echo "Bad or missing operator"
       breaksw
     endsw

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-13-2014
Thank you, Carona. I'll try that but would like to learn how to do it both ways. I'm learning UNIX through a book I bought and this was one of the exercises. Clearly - I'm not very good at it.
# 4  
Old 11-13-2014
Csh is not recomended: Csh Programming Considered Harmful

I'm more of a BASH scripter anyway.
BASH Programming - Introduction HOW-TO

Hope this helps

Last edited by sea; 11-13-2014 at 11:50 PM..
# 5  
Old 11-14-2014
I'm using it as part of an exercise in the book. In this respect, I want to use it (or tcsh)
# 6  
Old 11-14-2014
Quote:
Originally Posted by ksmarine1980
Thank you, Carona. I'll try that but would like to learn how to do it both ways.
I told you both ways.
Quote:
Originally Posted by ksmarine1980
I'm using it as part of an exercise in the book. In this respect, I want to use it (or tcsh)
CSH is not UNIX, it's a nonstandard language which was popular in the early 80's(it supported "fancy" things like line editing!) but has only clung to a weird, tenuous life since then -- mostly through it being forcefed to new students through badly out-of-date textbooks. Occasional old systems still run it to use scripts old enough to be having mid-life crises. Kind of the UNIX equivalent of qbasic.

These days a proper UNIX shell has all the same advantages with none of the notorious design flaws.

Last edited by Corona688; 11-14-2014 at 11:39 AM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 11-14-2014
I generally refuse to use csh and its derivatives for reasons already mentioned in this thread... But, I think you want to change:
Code:
else if ( $2 =~ "times" ) then
        @ c = $1 * $3
        echo "The product is: $c"

else if ( ( $2 !~ "plus" || $2 !~ "minus" ) && ( $2 !~ "div" || $2 !~ "times" ) ) then
        echo "You have entered an invalid operation"

else
        echo "You are missing an operand or operator"
endif

to something like:
Code:
else if ( $2 =~ "times" ) then
        @ c = $1 * $3
        echo "The product is: $c"

else    echo "You have entered an invalid operation"
        exit 2
endif

and I think you want to add something like:

Code:
if ( $# < 3 ) then
        echo "You are missing an operand or operator"
        exit 1
endif

before the code you showed us. I have not tested any of these suggestions, but they should get you a little bit closer to what you're trying to do.

If you want to learn about UNIX and Linux (and BSD) system shell programming, I strongly suggest you get a different book that describes ksh, bash, or some other shell that is based on Bourne shell syntax.

Good luck.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Pls help with script made a calculator

Hello, I'm in need with a little help for my script please this is the brief i need to complete which I haven't been able to do: On option 7 stop the calculator The calculator will keep running until option 7 is chosen. Any other option than 1-7 will generate an error message. Pls any help... (1 Reply)
Discussion started by: linuxepicuser
1 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Csh script

Hey all, I've only just started using UNIX coding on my Masters project, so am still learning!! The script I've been writing is literally just for me to get used to writing it and seeing what I can do with some data I've been given. I'm trying to write a script, where the penultimate line... (2 Replies)
Discussion started by: southernlight
2 Replies

5. Shell Programming and Scripting

csh script help

Hey I am brand new to this forum and scripting. I have several documents (1000+) all formated exactly the same. Each document contains 97 lines. I want to pull 3 lines from the documents to populate a file. These 3 lines are line number 9, 24, and 58. Ok my questions: Instead of using... (3 Replies)
Discussion started by: david6789
3 Replies

6. 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

7. Shell Programming and Scripting

how to source csh script in tcl script

i have atcl script and i want to source a csh script to reflect changes done by script ........ Please help....... (0 Replies)
Discussion started by: paragarora47
0 Replies

8. Shell Programming and Scripting

Help with csh script

Ok I asked something similar earlier with no response, so maybe I didn't word it correctly. I'm new at this, so thank you for your help. Here's what I have right now. ---------------------------- > cat MySourceFile #!/bin/csh echo "Please Enter Value For My_Env_Var:" set answer = $< ... (1 Reply)
Discussion started by: MMorrison
1 Replies

9. Shell Programming and Scripting

Simple CSH script

Hi everyone, I have never wrote script in csh before, but I need to add only few lines to an existing one. I tried to use the bash standard syntax, and it did not work. So, I attempted to use csh syntax, and it is not working. Can someone help please: switch ( $Return_Code ) case 0:... (3 Replies)
Discussion started by: nimo
3 Replies

10. Shell Programming and Scripting

csh failing to call an 2 embedded csh script

I have an extraordinary problem with a csh script.....(feel free to berate the use of this but I'm modifying an existing bunch of them) Anyway, I have a master csh script which in turn calls a second csh script. This second csh script is below. Within this second script are two compiled C++... (1 Reply)
Discussion started by: pollsizer
1 Replies
Login or Register to Ask a Question