Linux Script Basics
Prev | Chapter 6. Scripting | Next

## Linux Script Basics

We summarize some scripting rules here for those who may not be familiar with Linux scripting. If you do not understand the rules or the syntax or have questions then you should consult Linux documentation available on the internet.

On Linux, all commands typed in a console window are read and interpreted by a command interpreter called a shell program. The are several shell programs available for Linux but we use bash exclusively. If you elect to use a different shell program, like csh, then you will probably need to modify our scripts.

If you do not set execute permissions you must submit it to the shell for execution. If you set execute permissions on a script file then you can execute it directly by typing the filename. You can set execute permission on Linux with the chmod utility as shown in the example below.

    # bash script.sh
    ...
    # chmod 0755 script.sh
    # script.sh
    ...

The example above show how to run a script file using bash when it does not have execute permissions set. It then shows how to set the execute permissions and run it directly without using bash.

It is good practice to insert a bang-path on the first line of a script to ensure is is executed by the correct shell program, regardless of the shell used to open the script. It is possible to open a script with one shell and have it execute under another shell.

    #!/bin/bash

The next example shows an example bang-path. The first two characters inform the shell that this file contains scripting commands. The remainder of the bang-path line includes a program path with any options and arguments needed by the program. The shell will invoke that program and pass the rest of the script file to the program as standard input.

Symbols are defined with the symbol name then an equals sign then the definition text. By convention, symbol names use uppercase letters. The definition consists of all characters from the equals sign to the line end. If a definition contains white space, enclose it in quotes so that it is treated as one string when expanded. Symbol scope ends with the defining file unless it is exported.

    NIC=eth0
    MAC=00:B0:52:00:12:34
    MFG="Intergalactic Software Pirates, Inc."
    ...
    echo NIC is ${NIC} MAC is ${MAC} MFG is ${MFG}

To reference a symbol, precede the variable name with a dollar sign. Atheros scripts go one step further by enclosing the symbol name is braces. This improves readability and simplifies global search-and-replace operations.

Prev | Up | Next
Chapter 6. Scripting | Home | Host Independence