PDA

View Full Version : n00bie shell scripting question




phiber optik
Jul 28, 2006, 08:51 PM
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
Jul 28, 2006, 11:28 PM
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:

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

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

But the former method is preferred.

slooksterPSV
Jul 29, 2006, 06:18 AM
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:

$ man command

e.g. for help on echo

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

$ 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
Jul 29, 2006, 06:35 AM
And if pico, emacs and vi gets a little scary, you can always try TextWrangler (http://www.barebones.com/products/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
Jul 29, 2006, 10:00 AM
They can also be run like executables, assuming you've chmod'd the script with execution privileges. So, optionally, you can do the following:

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


#!/bin/sh

reh
Jul 29, 2006, 11:03 AM
Edit: Where's my blasted delete button... It's too early!

savar
Jul 29, 2006, 03:22 PM
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.