basic script question.

n1cster

Registered
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.
 
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).
 
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.
 
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'.
 
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!
 
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")
 
Back
Top