Previous Section  < Day Day Up >  Next Section

12.1. Introduction

When commands are executed from within a file, instead of from the command line, the file is called a shell script and the shell is running noninteractively. Writing Korn shell scripts requires a few steps, as outlined in the following section.

12.1.1 The Steps in Creating a Shell Script

A shell script is normally written in an editor and consists of commands interspersed with comments. Comments are preceded by a pound sign (#).

The First Line

At the top left corner, the line preceded by #! (often called shbang) indicates the program that will be executing the lines in the script. In Korn shell scripts, this appears as


#!/bin/ksh


The #!, also called the magic number, is used by the kernel to identify the program that should be interpreting the lines in the script. This line must be the top line of your script. The Korn shell also provides a number of invocation options that control how the shell behaves. These options are listed at the end of this chapter in "Korn Shell Invocation Arguments" on page 740.

Comments

Comments are lines preceded by a pound sign. They are used to document your script. It is sometimes difficult to understand what the script is supposed to do if it is not commented. Although comments are important, they are often too sparse or not even used at all. Try to get used to commenting what you are doing, not only for someone else, but also for yourself.

Executable Statements and Korn Shell Constructs

A Korn shell program consists of a combination of UNIX/Linux commands, Korn shell commands, programming constructs, and comments.

Naming and Storing Scripts

When naming scripts, it is a good idea to give the script a meaningful name that does not conflict with other UNIX/Linux commands or aliases. For example, you may want to call the script test because it is merely performing some simple test procedure, but test is a built-in command and you may find you are executing the wrong test. Additionally, if you name the file foo, goo, boobar, and so forth, in a few days or even hours you may not have any idea what is in that script!

After you have tested your script and found it bug-free, make a directory where you can store the scripts, then set the path so that your scripts can be executed from anywhere in the directory hierarchy.

Example 12.1.

1   $ mkdir ~/bin

2   $ mv myscript ~/bin



(In .profile)

3   export PATH=${PATH}:~/bin

4   $ . .profile


EXPLANATION

  1. A common place to store scripts is in a directory under your home directory called bin.

  2. The script, called myscript, is moved into the new bin directory.

  3. The new directory is added to the PATH variable in the .profile initialization file.

  4. The dot command causes the .profile file to be executed in the current environment so that you do not have to log out and then back in to enable the new setting.

Making a Script Executable

When you create a file, it is not automatically given execute permission (regardless of how umask is set). You need this permission to run your script. Use the chmod command to turn on execute permission.

Example 12.2.

1   $ chmod +x myscript

2   $ ls -lF myscript

    -rwxr--xr--x  1 ellie      0 Jul 12 13:00 myscript*


EXPLANATION

  1. The chmod command is used to turn on execute permission for the user, group, and others.

  2. The output of the ls command indicates that all users have execute permission on the myscript file. The asterisk at the end of the filename also indicates that this is an executable program.

Using a Script As an Argument to ksh

If you don't make a script executable, you can execute it by passing it as an argument to the ksh command.

Example 12.3.

(The Command Line)

$ ksh myscript


EXPLANATION

If the ksh program is given a script name as its argument, it will execute the script and the #! line is not necessary or even used.

A Scripting Session

In Example 12.4, the user will create a script in the editor. After saving the file, the execute permissions are turned on, and the script is executed. If there are errors in the program, the Korn shell will respond immediately.

Example 12.4.

(The Script)

1   #!/bin/ksh

2   # This is the first Korn shell program of the day.

    # Scriptname: greetings

    # Written by: Karen Korny

3   print "Hello $LOGNAME, it's nice talking to you."

4   print "Your present working directory is $(pwd)."

    print "You are working on a machine called $(uname -n)."

    print "Here is a list of your files."

5   ls       # List files in the present working directory

    print "Bye for now $LOGNAME. The time is $(date +%T)!"



(The Command Line)

    $ chmod +x greetings

    $ greetings

3   Hello karen, it's nice talking to you.

4   Your present working directory is /home/lion/karen/junk

    You are working on a machine called lion.

    Here is a list of your files.

5   Afile           cplus     letter       prac

    Answerbook     cprog      library      prac1

    bourne          joke      notes        perl5

    Bye for now karen. The time is 18:05:07!


EXPLANATION

  1. The first line of the script, #!/bin/ksh, lets the kernel know what interpreter will execute the lines in this program.

  2. The comments are nonexecutable lines preceded by a #. They can be on a line by themselves or inserted in a line after a command.

  3. The print command displays the line on the screen, after variable substitution is performed by the shell.

  4. The print command displays the line on the screen, after command substitution is performed by the shell.

  5. The ls command is executed. The comment, any text on the line after the pound sign (#), will be ignored by the shell.

    Previous Section  < Day Day Up >  Next Section