Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

slooksterPSV

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
NOTE & DISCLAIMER:
THIS SHOULD NOT BE ATTEMPTED BY ANY BEGINNER USER OR ANY USER WITH CRITICAL DATA. THE FOLDERS I DIVE INTO ARE RESPECTIVELY THE MOST IMPORTANT FILES IN MAC OS X FOR THE UNIX SIDE. IF THESE FILES ARE REMOVED, DELETED, OR OVERWRITTEN YOU MAY NOT BE ABLE TO RECOVER THEM THUS CAUSING YOUR SYSTEM TO FAIL. DO NOT ATTEMPT THIS UNLESS YOU KNOW WHAT YOU ARE DOING. I AM NOT RESPONSIBLE FOR WHAT YOU DO TO YOUR SYSTEM.

Now that we got that out of the way here's what I did:
I always open up terminal to do quick calculations like adding or subtracting or whatever, but there's not way to do it (at least I don't know how to without using python or that). Calculator takes too long to get to, even by Spotlight, and the Widgets are just too much of memory hogs, so here's what I did:
I made a python program - calc.py:
Code:
#!/usr/bin/env python

import sys
num = float(sys.argv[1]) + float(sys.argv[2])
print sys.argv[1]," + ",sys.argv[2]," = ",num
num = float(sys.argv[1]) - float(sys.argv[2])
print sys.argv[1]," - ",sys.argv[2]," = ",num
num = float(sys.argv[1]) * float(sys.argv[2])
print sys.argv[1]," * ",sys.argv[2]," = ",num
num = float(sys.argv[1]) / float(sys.argv[2])
print sys.argv[1]," / ",sys.argv[2]," = ",num

and my calc sh program
Calc - nope its not calc.sh its just calc
Code:
python /usr/bin/python_apps/calc.py $1 $2

I moved calc into /usr/bin
I created a directory in /usr/bin called python_apps
I then moved calc.py into /usr/bin/python_apps

I had to sudo to do those 3 things (or you can su into it).

Now I just type in this to receive these results:
Code:
Shawn-Barnes-iBook-G4:~ sbarn$ calc 10.252 284.24
10.252  +  284.24  =  294.492
10.252  -  284.24  =  -273.988
10.252  *  284.24  =  2914.02848
10.252  /  284.24  =  0.0360681114551
Shawn-Barnes-iBook-G4:~ sbarn$

Now I can make other Python utilities/programs and link them to an SH program, I could even do a menu program to access various utilities.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
No offense, but this isn't what I would consider "Advanced" - in fact, that script is harmless so why the BIG DISCLAIMER?

Couple of suggestions for you.

Validate your inputs are numeric
Check for zero before dividing

Congrats on your util script and welcome to the world of shell programming.
 

slooksterPSV

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
kingjr3 said:
No offense, but this isn't what I would consider "Advanced" - in fact, that script is harmless so why the BIG DISCLAIMER?

Couple of suggestions for you.

Validate your inputs are numeric
Check for zero before dividing

Congrats on your util script and welcome to the world of shell programming.
If they made a program and put in the folder and overwrote a program that the OS needed, it'd be pretty bad. Advanced, I'm was going for Advanced users, beginners I wouldn't recommend they do this just for that reason of overwriting a file. It was late in the night I think, when I posted this. I dunno I thought it was pretty good =P. Darn Calculators take so long to load.
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Nice script, but if I want to calculate in the Terminal, I just write python and that gives me a nice python shell that lets me calculate to my heart's content.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
gekko513 said:
Nice script, but if I want to calculate in the Terminal, I just write python and that gives me a nice python shell that lets me calculate to my heart's content.

You all should try 'bc'...its a very good calculator and would save both of you a trip to python.
 

slooksterPSV

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
savar said:
You all should try 'bc'...its a very good calculator and would save both of you a trip to python.
OMG *hugs savar* that's is so cool!!! I like that, that helps so much.

Someone needs to make a sticky for Terminal commands:
e.g.:
echo - displays whatever is typed after echo
bc - calculator
vim - text editor
pico - text editor
etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.