[Newbies] PipeableOSProcess shell interaction
David T. Lewis
lewis at mail.msen.com
Wed Jun 12 12:47:50 UTC 2013
On Tue, Jun 11, 2013 at 10:34:47PM -0700, Sean P. DeNigris wrote:
> When using PipeableOSProcess as a shell, how can I get feedback on the status
> of the individual commands?
>
> For example, with waitForCommand:, I can do:
> p := PipeableOSProcess waitForCommand: 'curl -L largeFile'.
> p succeeded ifFalse: [ ^ self error: 'Could not dowload...' ].
>
> but a the shell like:
> shell := PipeableOSProcess bash.
> shell exec: ('cd ', folderName).
> shell exec: 'curl -L largeFile...'.
> what's the equivalent of #succeeded? If there's no clear external sign that
> the command is finished, how do I check? Also, I don't seem to get anything
> back on the error stream e.g. if I change curl to cul, which doesn't exist,
> I still get an empty string from #errorUpToEnd.
>
When you run a Unix shell (bash) in a pipeable proxy, you can talk to the
shell by reading and writing to the proxy. A Unix shell keeps track of the
exit status of the last program that it ran in a shell variable ($?), so
you can ask the shell for that exit status.
This should give you a few ideas:
shell := PipeableOSProcess bash. "Run a bash shell in a pipeable proxy"
shell pipeFromOutput reader setNonBlocking. "Do not block the VM on reads"
shell exec: 'cd ..'. "Write a command to shell input with a line terminator"
shell exec: 'ls'. "Write another command to bash"
(Delay forMilliseconds: 200) wait. "Wait for output to arrive"
shell upToEnd inspect. "Read from bash shell output"
shell exec: 'echo $?'. "Ask bash shell for exit status of the last command".
(Delay forMilliseconds: 100) wait.
shell upToEnd inspect.
shell close. "Do not leave open file handles from pipes"
PipeableOSProcess is designed to be part of CommandShell, so when you use it
directly (rather than using a CommandShell), you need to take care of a few
details. The important things are to set the output pipe for nonblocking reads
(otherwise you will hang up your VM on a read), and to close pipes when you
are done using them (otherwise you will "leak" file handles over time).
Dave
More information about the Beginners
mailing list