QShell for iSeries
Authors: Holt T. Kulack F. A.
Published year: 2003
Pages: 26-30/219
Buy this book on amazon.com >>

Comments

At this point, you should know how to create and revise stream files in your directory. You have only one thing left to learn: what to put in those stream files. That is what the rest of this book is all about.

Since every good program requires documentation, one of the first things you should learn is how to include comments in Qshell source code.

Comments begin with the # symbol and span the remainder of the line. The # symbol is referred to by various names , including " pound ," "hash," "sharp," and "number." Comments are ignored in Qshell scripts, as are blank lines.

A comment may occupy the entire line or the trailing portion only. Here are examples of both types of comments:

# If the -p option was specified, print the output.
shift $OPTIND-1          # shift out the options



The Magic Number

If you look at scripts developed for Unix systems, you will frequently see a special comment on the first line. This comment begins with the symbols # and ! (often pronounced " pound bang"), and is called the magic number . The purpose of the magic number is to tell Unix which interpreter to load to process the script.

The magic number is discussed in detail in chapter 12. For now, you can ignore it.



Executable Commands

You are probably aware that some CL commands are permitted in CL programs only, other CL commands are permitted to run interactively only, and still others may run in either environment. For example, the IF command is only allowed in CL programs, the GO command runs in interactive jobs only, and SBMJOB works in either environment.

Qshell commands are not restricted in that way. All Qshell commands may run in an interactive session or in a script (although it may not always make sense to enter a certain command in a certain environment).

If you enter a syntactically incomplete command in an interactive session, Qshell responds with the secondary prompt. You can continue to enter line after line of commands, until Qshell determines that execution is possible.



Special Scripts

When you begin a Qshell session, it automatically executes the following three script files, if they exist:

  • The global profile file, /etc/profileThe system administrator uses this file to set system-wide options for all users.

  • A file named .profileAfter running /etc/profile, Qshell looks in the user 's home directory for this profile, which is used for personal customization. (Yes, it begins with a period, and is pronounced "dot profile.") The .profile file is a good place to define environment variables , including the ENV environment variable. Use the DSPUSRPRF command to display your HOME directory. For example, DSPUSRPRF JSMITH.

  • The file named in the ENV environment variableQshell looks to see if the ENV environment variable has a value. If so, and if that value is the name of an existing file, Qshell executes the file. One of the most common uses of ENV is to define aliases , which are short names for a command. (Aliases are discussed in detail in chapter 12.)

Qshell runs these script files in the order given here, and in the current process.



Your First Script

Before you proceed, be sure you know how to edit and run Qshell scripts. Figure 4.2 is a script you can enter and run for practice.

start figure

# Getting acquainted
print "Please enter your name."
read name
print "Hi, $name. Qshell rulz!"

print "What's your favorite programming language?"
read lang
if [ -z "$lang" ]
   then print "Maybe we can talk at a more convenient time."
        exit 1
fi

lang=$(print $lang  tr '[:lower:]' '[:upper:]') # convert to caps
case $lang in
   *RPG*) print "Me too!";;
   COBOL*) print "How quaint!";;
   JAVA) print "At least you didn't say \"C\"";;
   C) print "You're a glutton for punishment!";;
   *) print "To each his own."
esac

end figure

Figure 4.2: Use this script for practice.

To enter and run the script, use the following process:

  1. Sign on to an iSeries machine through a terminal or a 5250 emulation session.

  2. If necessary, change to the directory where you are going to place the Qshell script.

  3. At a CL command line, type qsh and press Enter to begin a Qshell session.

  4. Since you cannot edit a file directly from Qshell, press the F21 key to get a CL command line.

  5. Use EDTF to enter the Qshell script. If you are not sure how to use EDTF, see chapter 3.

  6. After editing, press Enter from a blank line (or press F12) to close the command-line window and return to Qshell.

  7. If necessary, use chmod to make the script executable.

  8. Type the name of your script on the Qshell command line and press Enter to run it.

If you must edit the script, press F21 to get back to the CL command line. From there, you can use the F9 key to retrieve the previous EDTF command.

Continue running and editing the script in Figure 4.2 as needed, until you have no syntax errors and understand how the script works. Don't worry if you don't understand the syntax of the Qshell commands at this point. That's the purpose of the rest of the book.


QShell for iSeries
Authors: Holt T. Kulack F. A.
Published year: 2003
Pages: 26-30/219
Buy this book on amazon.com >>

Similar books on Amazon