Proper way to write and execute a terminal script?

michaelsanford

Translator, Web Developer
I've got some commands that parse out my web logs into readable, catgorized columns suitable for other post-processing (excel, MySQL, whatever).

The only problem is that to rip apart my logs I'm entering a bunch of command sequentially into the terminal. Obviously this is stupid, but I don't know how to properly write a PERL ( ? ) or tcsh script, as I would an AppleScript or an old DOS BAT.

All I know is that the script has to take the example form
Code:
#!/usr/bin/sh
ls | awk '{print $1}'
After that, I don't know how to actually run the script without using
Code:
tsch [script name]
Not to mention how to run the script from a Cocoa app, but I'm still trying to figure out how to make Cocoa apps interact with Darwin :p

Tips on running the script in the terminal?
There's got to be a better way than the second code example?

Thanks!
 
OK, a couple of points

If the first line of the script starts #! then the script will be run using whatever program is pointed to after that. So if you write a tcsh script, use:
#!/bin/tcsh

If you write an sh script (the syntax is a bit different in some cases, I'm more comfortable with tcsh):
#!/bin/sh

To find the location of commands on your computer, use the command "which":
Code:
$ which perl
/usr/bin/perl

To make the script executable, use
Code:
chmod a+x script
then you can just execute it directly, rather than having to execute a shell and give it the script as an argument.

If you copy the script to somewhere that's in your path, then you can just call it without giving an explicit path to it. In my case

Code:
echo $path
/Users/mark/bin/powerpc-apple-darwin /Users/mark/bin /sw/bin /usr/local/bin /usr/bin /bin /sw/sbin /usr/local/sbin /usr/sbin /sbin /usr/X11R6/bin /Developer/Tools

So I could move or copy the script to (for example) /Users/mark/bin Then I could just execute the script by typing its name.
 
Back
Top