PDA

View Full Version : basic script question.


n1cster
February 27th, 2009, 06:18 AM
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.

xserve@home
March 23rd, 2009, 11:28 PM
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).

Mikuro
March 24th, 2009, 12:05 AM
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.

fintler
March 26th, 2009, 02:52 PM
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'.

xserve@home
March 26th, 2009, 04:26 PM
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!

artov
March 27th, 2009, 04:55 PM
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")

ElDiabloConCaca
March 27th, 2009, 05:42 PM
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."