image
image

Go Back   macosx.com > Scripts > Shell/Terminal Scripts

Reply
 
LinkBack Thread Tools
  #1  
Old February 27th, 2009, 06:18 AM
Registered User
 
Join Date: Feb 2009
Posts: 5
Thanks: 7
Thanked 0 Times in 0 Posts
n1cster is on a distinguished road
basic script question.

Very newbie question sry but here goes:-

What format do i have to save a script file as and how do i run a script from terminal?

cheers.
Reply With Quote
  #2  
Old March 23rd, 2009, 11:28 PM
Just Some Guy
 
Join Date: Feb 2009
Location: LA
Posts: 21
Thanks: 0
Thanked 3 Times in 3 Posts
xserve@home is on a distinguished road
Shell scripts (like Bash) are just text files. You will have to set the permissions to give yourself execute access (Use the chmod command). Also when you execute your script be sure that the script is store in a directory on your search list or if the script is in you current directory use the ./ path before the scriptname (or add the ./ directory to your search list).
Reply With Quote
The Following User Says Thank You to xserve@home For This Useful Post:
n1cster (March 24th, 2009)
  #3  
Old March 24th, 2009, 12:05 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,682
Thanks: 6
Thanked 53 Times in 48 Posts
Mikuro will become famous soon enough
Another thing to watch out for is the line break format. Scripts must use Unix-style line feeds (LF). Script-oriented text editors like TextWrangler let you easily see and change the line break style.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.8

Useful programs: Privoxy, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
The Following User Says Thank You to Mikuro For This Useful Post:
n1cster (March 24th, 2009)
  #4  
Old March 26th, 2009, 02:52 PM
fintler's Avatar
Registered User
 
Join Date: Sep 2000
Posts: 31
Thanks: 0
Thanked 1 Time in 1 Post
fintler is on a distinguished road
Although you can usually run a script by typing a "./" before the script name, you can also run it by passing it as an argument to the shell. Like 'bash scriptname.sh'.
Reply With Quote
The Following User Says Thank You to fintler For This Useful Post:
n1cster (April 2nd, 2009)
  #5  
Old March 26th, 2009, 04:26 PM
Just Some Guy
 
Join Date: Feb 2009
Location: LA
Posts: 21
Thanks: 0
Thanked 3 Times in 3 Posts
xserve@home is on a distinguished road
Fintler:

True. What you are doing then is spawning a new process ("bash"), just to run a script. And sometime there are good reasons for doing so. In the olden days, it would have seemed unnecessary overhead. Fast computers today -- poof!
Reply With Quote
The Following User Says Thank You to xserve@home For This Useful Post:
n1cster (April 2nd, 2009)
  #6  
Old March 27th, 2009, 04:55 PM
Registered User
 
Join Date: Mar 2005
Posts: 252
Thanks: 0
Thanked 5 Times in 5 Posts
artov is on a distinguished road
About "format". Generally shell scripts start with line

#!/bin/sh

The #! is called shebang (for some reason), and it means to kernel that

- the file is a script
- the script is interpreted by program that is after the shebang

You run the script either

- by giving its name: name-of-the-script
- by giving its path : /dir/subdir/name-of-the-script
- by giving source command to your current shell (either . or source, depending on the shell you are using)

The first case works only if the script is on place named in $PATH environment file. The first and second case work only if you have set the execution bit on the file (using command "chmod u+x the-file-name").

But, if you meant the extension by the shell script, there is no restriction, but normally the script is named either without extension ("myfinescript") or with extension that reminds that it is shell script ("myanotherscript.sh")
Reply With Quote
The Following User Says Thank You to artov For This Useful Post:
n1cster (April 2nd, 2009)
  #7  
Old March 27th, 2009, 05:42 PM
ElDiabloConCaca's Avatar
Registered User
 
Join Date: Aug 2001
Location: San Antonio, Texas
Posts: 12,678
Thanks: 7
Thanked 388 Times in 370 Posts
ElDiabloConCaca is a glorious beacon of lightElDiabloConCaca is a glorious beacon of lightElDiabloConCaca is a glorious beacon of lightElDiabloConCaca is a glorious beacon of lightElDiabloConCaca is a glorious beacon of lightElDiabloConCaca is a glorious beacon of light
Quote:
Originally Posted by artov View Post
The #! is called shebang (for some reason), and it means to kernel that
Actually, it's called "hash-bang." The "#" is called "hash," and the "!" is called "bang." Sometimes people shorten it to "she-bang."
__________________
Mac mini 2.0GHz 10.6.2 • 4GB • 320GB • Superdrive • 4 x 1TB USB 2.0 • LED Cinema Display
MacBook 2.0GHz Core 2 Duo - White 10.6.2 • 4GB • 250GB • CD-RW/DVD-ROM
iPhone 3G 8GB • iPod Touch 8GB • iPod Photo 60GB • iPod nano 1GB • AT&T U-Verse 18Mb/2Mb
http://www.jeffhoppe.com
Reply With Quote
The Following User Says Thank You to ElDiabloConCaca For This Useful Post:
n1cster (April 2nd, 2009)
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump


All times are GMT -5. The time now is 06:12 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC1
Copyright 2000-2010 DigitalCrowd, Inc.