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

phiber optik

macrumors newbie
Original poster
Apr 25, 2006
17
0
hey all
just a quick question about shell scripting. im reading all this stuff bout using different commands to do ceritan things, but it never tells you how to execute them. do you write your script in something like TextEdit and then convert it into an exacutable? how would you do that? if thats not how you do it, then please enlighten me.
thanks for any input
phiber
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Shell scripts can be written in something like TextEdit (assuming you have plain text on!), or vi, or emacs, or pico, or Xcode. They are executed on the command line, using whichever shell you wish. The typical shell for running shell scripts is just plain old "sh". So on the command line, you do the following:

Code:
$ sh myscript.sh
<output here>

They can also be run like executables, assuming you've chmod'd the script with execution privileges. So, optionally, you can do the following:

Code:
$ chmod a+x myscript.sh
$ ./myscript.sh
<output here>

But the former method is preferred.
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
I prefer the latter method, but that's just me.
If you need to know how to execute a specific command on the terminal type in something like this:
Code:
$ man [i]command[/i]
e.g. for help on echo
Code:
$ man echo
$ denotes the prompt itself, it is not a character you add.

Personally, also, I use pico although a lot of advanced users like vi or vim.
e.g.
Code:
$ pico test.sh
 GNU nano 1.2.4                    File: test.sh                               




















                                  [ New File ]
^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Page ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where Is  ^V Next Page ^U UnCut Txt ^T To Spell
CTRL is the ^
That's the pico interface. Since terminals are text interfaced, that's what you work with.
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,661
1,469
Bergen, Norway
And if pico, emacs and vi gets a little scary, you can always try TextWrangler. A very nice, free text editor.

You'll even find a couple of Hello World scripts in different scripting languages under the #! menu. :)
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
kpua said:
They can also be run like executables, assuming you've chmod'd the script with execution privileges. So, optionally, you can do the following:

Code:
$ chmod a+x myscript.sh
$ ./myscript.sh
<output here>

This will only work if you specify the shell to use in the first line of the script. For sh shell, the first line of the script should be:

Code:
#!/bin/sh
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
phiber optik said:
hey all
just a quick question about shell scripting. im reading all this stuff bout using different commands to do ceritan things, but it never tells you how to execute them. do you write your script in something like TextEdit and then convert it into an exacutable? how would you do that? if thats not how you do it, then please enlighten me.
thanks for any input
phiber

just for the record: anything you can put into a shell script you can also type at the command line -- no script necessary. even conditional structures and loops can be used at the command line.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.