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

Discussion in 'Mac Programming' started by slooksterPSV, Jun 22, 2006.

  1. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #1
    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.
     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    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.
     
  3. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #3
    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.
     
  4. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #4
    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.
     
  5. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    You all should try 'bc'...its a very good calculator and would save both of you a trip to python.
     
  6. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #6
    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.
     

Share This Page