Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 44.3 What's a Shell, Anyway? Chapter 44
Shell Programming for the Uninitiated
Next: 44.5 Test String Values with Bourne Shell case
 

44.4 Testing How Your System Executes Files

Your version of UNIX may understand the #! notation. This is a way to tell UNIX which shell should execute the commands in your file. [1] If your UNIX doesn't recognize #!, you'll need to be sure that you know how to make it read shell scripts using the Bourne shell - regardless of the shell you use interactively - because most scripts in this book are for the Bourne shell.

[1] Actually, you can use #! to specify any interpreter program (45.3), not just a shell.

To test your system, let's make a two-line file named testing.

NOTE: Do not make programs named test. There's an important system command named test (44.20), and your command might be used, accidentally, instead of the system program. Name your test programs testing, atest, whatever - just not test. Article 44.21 shows how to find a unique filename.

  1. Make a file named testing (use an editor, or just make the file by hand with cat > testing (25.2)). Put the following two lines in the file. Be sure to start on the first line of the file, and type this text just as it's shown. Be sure that the hash mark (#) is at the left-hand edge (column 1) of the first line:

    #!/bin/echo just
    export stuff

  2. Exit the editor and save the file. Make the file executable by typing chmod +x testing (44.2).

Now run the program by typing its name at a shell prompt. There are four kinds of responses:

  1. If this happens, then the #! is working. You'll be able to tell your system which shell should run each script:

    % testing
    just testing
    %

    The answer just ./testing also means that #! is working. If you get an error like "testing: command not found," your current directory may not be in the shell's search path (8.7); try executing ./testing instead.

  2. If this happens, then your UNIX doesn't understand #!, but it ran your program with the Bourne shell anyhow:

    % testing
    %

  3. If this happens, then your system ran the program with an older version of the Bourne shell. You should not use comment lines starting with a hash mark (#):

    % testing
    #!: not found 
    %

  4. If this happens, then your UNIX doesn't understand #!, and it ran your program with the C shell:

    % testing
    export: Command not found.
    %

Many UNIX systems, especially newer ones, will answer just testing or just ./testing. That's because, as article 45.5 explains, the system strips off the #! from the start of the line, adds the script file's name (or pathname) to the end of it, and runs it:

/bin/echo just testing

(Article 8.6 covers the echo command.) If your system ran the shell script with the C shell, find a way to make it use the Bourne shell instead. Try the trick in article 45.6 or ask a local expert such as your system administrator.

- JP


Previous: 44.3 What's a Shell, Anyway? UNIX Power ToolsNext: 44.5 Test String Values with Bourne Shell case
44.3 What's a Shell, Anyway? Book Index44.5 Test String Values with Bourne Shell case

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System