PDA

View Full Version : How-To: Advanced Terminal Commands (Python & Shell)




slooksterPSV
Jun 23, 2006, 01:24 AM
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:

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

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:

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
Jun 26, 2006, 05:52 PM
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
Jun 26, 2006, 06:02 PM
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
Jun 26, 2006, 06:06 PM
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
Jun 26, 2006, 11:45 PM
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
Jun 27, 2006, 02:02 PM
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.