n00bie shell scripting question

Discussion in 'Mac Programming' started by phiber optik, Jul 28, 2006.

  1. phiber optik
    Expand Collapse
    macrumors newbie

    Apr 25, 2006
    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
  2. kpua
    Expand Collapse
    macrumors 6502

    Jul 25, 2006
    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.
  3. slooksterPSV
    Expand Collapse
    macrumors 68030


    Apr 17, 2004
    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 [i]command[/i]
    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.
    $ 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.
  4. Mitthrawnuruodo
    Expand Collapse
    Moderator emeritus


    Mar 10, 2004
    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. :)
  5. mrichmon
    Expand Collapse
    macrumors 6502a

    Jun 17, 2003
    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:

  6. reh
    Expand Collapse
    macrumors 6502a

    Oct 24, 2003
    Edit: Where's my blasted delete button... It's too early!
  7. savar
    Expand Collapse
    macrumors 68000


    Jun 6, 2003
    District of Columbia
    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.

Share This Page