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

ghettosteez

macrumors newbie
Original poster
Jun 12, 2011
1
0
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.
 

Hansr

macrumors 6502a
Apr 1, 2007
897
1
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.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
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.
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.