7.1 Shell Script Basics


7.1 Shell Script Basics

All Bourne shell scripts should start with the following line, indicating that the /bin/sh program should execute the commands in the script file:

 #!/bin/sh 

Make sure that no whitespace appears at the beginning of the script file. You can list any commands that you want the shell to execute following the #!/bin/sh line. Here is a very simple example:

 #!/bin/sh # # Print something, then run ls echo About to run the ls command. ls 

A # character at the beginning of a line indicates that the line is a comment; that is, the shell ignores anything on a line after a # . Use comments to explain parts of your scripts that are not easy to understand.

After creating a script and setting its permissions, you can run the script by placing the script file in one of the directories in your command path and then running the script name on the command line. Alternatively, you can run ./ script if the script is located in your current working directory.

7.1.1 Limitations of Shell Scripts

The Bourne shell manipulates commands and files with relative ease. You already saw the way the shell can redirect output in Section 1.14; this is one of the important elements of shell script programming. However, shell scripts are not the be-all and end-all of Unix programming. If you're trying to pick apart strings or do arithmetic computations , or if you want functions or complex control structures, you're better off using a scripting language like Perl, Python, or awk , or perhaps even a compiled language like C.

Be aware of your shell script sizes. Bourne shell scripts aren't meant to be big (though you will undoubtedly encounter some monstrosities in your time).




How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net