PDA

View Full Version : Automating terminal activities




ghettosteez
Jun 12, 2011, 02:50 PM
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...
./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
Jun 12, 2011, 03:14 PM
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:

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

chown33
Jun 12, 2011, 04:16 PM
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...
./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:
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

balamw
Jun 12, 2011, 05:02 PM
If and when your projects become more complex, you might also want to check out make, and create a makefile instead of a script.

http://en.wikipedia.org/wiki/Make_(software)

B

mobilehaathi
Jun 12, 2011, 08:58 PM
If and when your projects become more complex, you might also want to check out make, and create a makefile instead of a script.

http://en.wikipedia.org/wiki/Make_(software)

B
Make is where it's at!