Automating terminal activities

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

  1. macrumors newbie

    Jun 12, 2011
    I code, compile, and run a lot of test scripts through terminal using gcc and find myself typing something like the following a lot:
    cd /users/me/desktop
    gcc test.c
    And, assuming everything compiles correctly...
    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

    Apr 1, 2007
    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 (or .command) text document on the Desktop with:

    gcc ~/Desktop/test.c -o ~/Desktop/test.out
    Then set it to executable (chmod 755 ~/Desktop/test.out) and make sure it's set to open with then you can just double click it when you need to compile.
  3. macrumors 604

    Aug 9, 2009
    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.

    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


    Staff Member

    Aug 16, 2005
    New England
  5. macrumors 603


    Aug 19, 2008
    The Anthropocene
    Make is where it's at!

Share This Page