Chapter 7. Console Programming and the Shell


THERE ARE TWO BASIC TYPES OF applications you can create using REALbasic: a desktop application and a console application. You have already seen how to create a desktop application in Chapter 5 and now, in this chapter, you will see how to create a console application. The capability to create console applications requires the Professional version of REALbasic. However, if you do not have the Professional version, you can still execute the examples, but they will run for only 5 minutes at a time and work for 10 days.

Prior to introducing console applications, however, I will introduce the Shell class, which is a class that can be used in either desktop applications or console applications, but which is related to the development of console applications in this way: console applications are applications that do not have a graphical user interface and are executed on the command line of the respective shells for each platform. The Shell class is a class that provides a means of causing such applications to be executed.

The Windows shell is different from the one used by Macintosh and Linux computers, and both have a different lineage. In Windows, the shell is referred to as the Shell, and you normally access it through the Command Prompt application (see Figure 7.1). If you have been around Windows for any significant amount of time, you will recognize the shell as the old DOS or Command prompt, which was the primary means of navigating folders and executing applications prior to the introduction of Windows.

Figure 7.1. Windows XP Shell.


Whereas on Windows the shell is the shell, on UNIX-like systems, there are several kinds of shells that you can use. Both Macintosh and Linux have terminal applications that you can use to access the shell, but the actual shell being accessed can varysh, bash, and tcsh are all different shells that can be used. In these examples, I'm using sh, although the other two are very common. The good news is that when using the Shell class, you can specify which shell to use, so that gives you some control. Otherwise, the systems themselves decide what the default shell is, and this might cause unintended consequences if you assumed one shell and got a different one.

A console application is one that can be executed from the shell. A console application is launched by typing the name of the application into the shell and pressing the Enter key. All shells have a core group of commands, which are really just small applications that perform some very basic tasks such as changing directories and moving and copying files. Arguments can also be passed to the application from the command line. Here's an example that is similar on both Windows and UNIX-like operating systems. First, take a look at the Windows version:

cd "C:\Program Files"


Figure 7.1 shows how the command looks when entered into a Windows Command Prompt application:

Next, the Macintosh and Linux versions:

cd /usr/local/bin


Figure 7.2 shows the same command entered into the Macintosh terminal application, using the bash shell, which is identified in the window title:

Figure 7.2. Macintosh Terminal, Bash Shell.


In both cases, you are changing into approximately equivalent directories. The "cd" stands for "change directory," and that is also the name of the command (or application) being executed. What follows is the argument, which is passed to the program when it is executed, much in the same way that arguments are passed to subroutines and functions.

In both examples, I just typed in cd and the program executed, but with most applications you normally would need to type in the actual path to the application, like this Linux example shows:

/usr/bin/cd /usr/local/bin


The reason you do not have to enter the entire path is because all systems have a PATH environment variable that lists the paths that should be checked whenever a command is entered on the command line that doesn't use an absolute path. By default, Windows and the UNIX-like systems are configured to be able to execute the cd command without the path. The environment variables used are usually user specific and can be customized. However, when using the Shell class, you cannot assume that the PATH has been set for anything.

When you create a desktop application, you design a graphical user interface, and this interface responds to user actions to determine what to do next. The user input comes in the form of mouse clicks or drags and the like, and by and large, the response to user input is displayed in the interface itselfin particular, windows and controls. Because a console application is not graphical, it needs another way to get user input, and another way to display or view the response to that inputsomething that is called output in console applications.

When you start a console application and include an argument in the command, you are using a special kind of input called standard input. The most common way to provide input to a console application is on the command line itself. After the application runs and computes whatever needs to be computed, it usually sends output to the console in the form of text. Sometimes the output is a status message, and other times the output is actually what the program does.

I'll show you an example later where you pass two arguments to a console application. The arguments are paths to particular files. The first argument points to an XML document and the second argument points to an XSLT stylesheet. The application takes that input, finds the files, performs the transformation, and sends the output to the console. This is called standard output. That's simple enough; the data that goes in is called standard input and the data that goes out is called standard output. There's also something called standard error, which is where error messages go (it so happens that it usually gets displayed in the same way as the standard output, but you can also designate a file for standard error data to be sent).

There are also some very interesting things that can be done with standard input and standard output. For example, the standard input into one program could be the standard output from another. This is called piping, and it's a technique you can use to send data through a series of transformations performed by a collection of applications.




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

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