# Proper way to write and execute a terminal script?



## michaelsanford (Nov 7, 2002)

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 
	
	



```
#!/usr/bin/sh
ls | awk '{print $1}'
```
After that, I don't know how to actually run the script without using
	
	



```
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 

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

Thanks!


----------



## scruffy (Nov 8, 2002)

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":

```
$ which perl
/usr/bin/perl
```

To make the script executable, use 

```
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


```
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.


----------



## michaelsanford (Nov 8, 2002)

Perfect! That's exactly what I needed to know, thanks a lot!


----------

