Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 1.2

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):

Download Icon 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 with ls -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 to more (or less, 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 uses DataAvailable and Completed to communicate with the program. In this mode, the shell works like a socket. The result 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 uses zsh, without the PATH 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.