Programmer's Reference

Usage example

This section contains examples using the UNIXProcess class. Evaluate each of the expressions by highlighting them and selecting Display from the Edit menu.

One common use for a UNIXProcess is to invoke shell commands from within Smalltalk and capture the output.

"Display on the Transcript the result of a shell command."
Transcript show: (UNIXProcess shell: 'ls -al')

To invoke a command and not capture the output, the system message is used.

"Perform a shell command and direct output to the start up terminal"
UNIXProcess system: 'env'

In some situations the shell: and system: messages do not provide sufficient control over the process. The following example shows the three stages of a UNIXProcess: creation, the reading and writing of data, and termination.

"Connect user defined streams to the UNIX process stdin, stdout
and stderr."
| process output input result |
 
process := UNIXProcess           "Create the process."
     execute: 'cat'
     input: true
     output: true
     error: nil
     environment: UNIXEnvironment current.
input := process stdin.
output := process stdout.
input
     nextPutAll: 'This is input sent into standard in';
     close.
process waitForCompletion.       "Wait for process to terminate."
output contents

Using the stream interface, it is possible to interact with a process. Future input to the process can be determined by the currently available output or state of the process.

"It is not necessary to provide all input immediately."
| process output input result |
process := UNIXProcess           "Create the process."
     execute: 'cat'
     input: true
     output: true
     error: nil
     environment: UNIXEnvironment current.
input := process stdin.
output := process stdout.
input
     nextPutAll: 'This';         "Perform read/writes of data."
     flush.
process isComplete ifTrue: [self error: 'Process should not be complete'].
input
     nextPutAll: ' is input sent into standard in';
     close.
process waitForCompletion.       "Wait for process to terminate."
output contents

The following code is an example method for an application. The method makes use of the UNIXProcess class to mail a message to a collection of recipients with a given subject. A method similar to this could be used to interface to the UNIX mail facilities from within Smalltalk.

Note:Highlighting the following example code and selecting Display will not work. It is a method and requires parameters.
"Send mail and ignore the reply."
mail2: recips subject: subject msg: string
| process args env |
args:= '/usr/ucb/mail'.
recips do: [:person | args := args, ' ', person].
env := UNIXEnvironment current.
 
process := UNIXProcess
     execute: args
     input: true
     output: true
     error: nil
     environment: env.
process stdin
     nextPutAll: 's ',subject; cr;
     nextPutAll: string; cr;
     close.
process waitForCompletion.
^process exitCode


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]