Automating terminal activities

Discussion in 'Mac Programming' started by ghettosteez, Jun 12, 2011.

  1. macrumors newbie

    Joined:
    Jun 12, 2011
    #1
    I code, compile, and run a lot of test scripts through terminal using gcc and find myself typing something like the following a lot:
    Code:
    cd /users/me/desktop
    gcc test.c
    And, assuming everything compiles correctly...
    Code:
    ./a.out
    Is there a way I can make a little program that sits on my desktop that runs all of this through the terminal with one click?

    I'm hoping it's something easy like a batch file that I can just make with a few lines in notepad.
     
  2. Hansr, Jun 12, 2011
    Last edited: Jun 12, 2011

    macrumors 6502a

    Joined:
    Apr 1, 2007
    #2
    If the name of the compiled file is always the same just make a shell script and make it executable then you can just double click it from the desktop.

    Create complile.sh (or .command) text document on the Desktop with:

    Code:
    !#/bin/sh
    gcc ~/Desktop/test.c -o ~/Desktop/test.out
    ~/Desktop/test.out
    Then set it to executable (chmod 755 ~/Desktop/test.out) and make sure it's set to open with terminal.app then you can just double click it when you need to compile.
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    The "if it compiles correctly" part is easy. The shell gives you conditional execution of a series of commands. Unconditional execution (plain sequential) uses semicolon or newline as the delimiter. Conditional execution (sequential on success, or sequential on failure) uses the && and || operators, which are NOT AT ALL similar to the single & or single | operators.

    Refer to the bash man page.

    Examples:
    Code:
    true && echo "Always true"
    gcc tryme.c && echo "Only if gcc worked" && ./a.out
    gcc botched.c || echo "gcc barfed"
    
    cd "$HOME/Desktop" && gcc test.c && ./a.out
    
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
  5. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #5
    Make is where it's at!
     

Share This Page