Article Preview
Buy Now
COLUMN
Wrap it up!
Using RB's Shell class to control UNIX.
Issue: 1.2 (October/November 2002)
Author: Didier Barbas
Article Description: No description available.
Article Length (in bytes): 11,223
Starting Page Number: 46
Article Number: 1124
Resource File(s):
1124.zip Updated: 2013-03-11 19:07:56
Related Link(s): None
Excerpt of article text...
Today, I will take you down the path of RB's shell classes for Unix/OS X. We will build a shell wrapper for the zip Unix tool. Adding other tools should be quite straightforward, and after reading this you should be able to unlock Unix's power through RB's shell classes.
Shell classes are available for Unix and DOS, meaning you can use them for OS X and Windows builds. As a stubborn Frenchman, I'll skip the subject of DOS shell classes since I don't care much about WinTel boxes. However, not everyone has the leisure to stay away from Windows, and building a shell class for DOS should be quite easy once you get the general principle. Have fun.
What is a Unix Shell?
A shell is a programm that accepts user input, sorts out the mess with more or less success, and passes it on to the kernel. There are several flavors, the one launched by default on the Terminal application being
tcsh
, an improved version of the C shell. The shell has no GUI, and shell commands should not rely on interactive user input. The most "natural" way of doing things is to pass all arguments on the command line. You can also route a command's output into another command, through a "pipe." For instance, when you list a directory's content withls -al
, however big your screen may be you will run into a directory that has way too many files, and you end up seeing only the last ones. To avoid that, you route the output of ls tomore (orless , which is even better):ls -al | less
However, it may happen that you need some user input during execution of the command. This is why RB shell classes have 3 modes: Synchronous, Asynchronous, and Interactive (0, 1, and 2). The first one is straightforward: you run the shell, and when it's over you read the result. The second one is a variation of the previous. You know you won't need any input, but you think the shell command might take some time to execute. You launch the shell command, and the program will be notified through the
completed
event when the command finishes. The last one usesDataAvailable
andCompleted
to communicate with the program. In this mode, the shell works like a socket. Theresult
property contains the output. Don't forget that Unix uses chr(10) and not chr(13) for the end-of-line character.However, there is another caveat: while the Terminal application uses the tcsh shell with a full complement of environment variables, including a
PATH
variable which lists default paths for programs, the shell class useszsh
, without thePATH
variable. So you have to know where the programs are before you can run them through a shell wrapper. The best thing to do (with standard programs like mysql, tar, zip, etc...) is to ask Unix where they are:which tar
, which should give you/usr/bin/tar
as the path to that utility.
...End of Excerpt. Please purchase the magazine to read the full article.