Key Features of Shell Scripts

Shell scripts are one specific subclass of interpreted programs, meaning that a special program (the interpreter— in this case, the shell program) reads the program file (the shell script) and implements the operations it specifies every time the program is run. This procedure is in contrast to compiled programs, in which a compiler reads the program file (the source code) and generates a new file (the object code or binary file) that the computer can run without further help from the compiler. Interpreted programs, and hence shell scripts, tend to be easier to develop because you can make a change to the program and run it immediately. Interpreted programs can also be run on any CPU, provided the appropriate shell exists and that the program doesn't depend upon specific hardware or CPU features. Compiled programs, by contrast, tend to run faster, because the compiler can generate object code that's optimized for the CPU; however, the resulting object code runs only on the target CPU or CPU family.

Shell scripts typically begin with a line that identifies them as such. This identification line begins with the characters #!, followed by the path to the interpreter (the shell itself). The Linux kernel knows to interpret these characters as identifying a script, and scripting languages all use a hash mark (#) as a comment character, so the shell ignores this line. Many shell scripts identify themselves as being run by/bin/sh, but some specify /bin/bash or some other shell. Some simple scripts can run on a wide variety of shells, but others use shell-specific features. For instance, bash and tcsh use different methods of assigning values to variables, so any script that does this will run under one shell or the other, but not both.

In order to run a shell script, you must either launch it by passing it explicitly to the shell (as in /bin/bash scriptname) or change the permissions on the script file so that it's executable. Thereafter, you can launch it just as you would any other executable program, by typing its name (possibly preceded by the complete path to the file). To make a file executable, you use the chmod command:

$ chmod a+x scriptname

Note See Chapter 5, "Doing Real Work in Text Mode," for more information on file permissions and the chmod command.

0 0

Post a comment