n00bie shell scripting question

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

  1. macrumors newbie

    Joined:
    Apr 25, 2006
    #1
    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
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    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.
     
  3. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #3
    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.
     
  4. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #4
    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. macrumors 6502a

    Joined:
    Jun 17, 2003
    #5
    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
    
     
  6. reh
    macrumors 6502a

    Joined:
    Oct 24, 2003
    Location:
    Arkansas
    #6
    Edit: Where's my blasted delete button... It's too early!
     
  7. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #7
    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