Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.67 KB

OS.md

File metadata and controls

44 lines (33 loc) · 1.67 KB

Basic interactions with the OS

This page describes basic interactions with the OS. The code snippet below should be working on all OS and are built-in: you do not need to install an external project to use them.

Execute shell command

The following code execute a shell command and returns an integer which is the return code of the executed command. If the return code is 0, the execution of the command is successful. Else, the integer returned is the error code generated by the shell command.

LibC uniqueInstance
	system: 'echo "foo" >> /tmp/foo.txt'

To retrieve the output of a command directly in Pharo, use #resultOfCommand:

LibC uniqueInstance
	resultOfCommand: 'echo "foo"'. "'foo
'"

Write environment variable

The environment behaves similarly to a dictionary when it comes to write a variable.

Smalltalk os environment at: 'FOO' put: 'bar'.
Smalltalk os environment at: 'FOO' ifAbsentPut: 'bar'

Note: Before Pharo 7, #setEnv:value: message was used. This message is part of the low-level API and should not be used directly.

Read environment variable

As for writing a variable, environment provide an API similar to Dictionary to read a variable.

Smalltalk os environment at: 'FOO'.
Smalltalk os environment at: 'FOO' ifAbsent: [ 'Nope :-(' ].
Smalltalk os environment at: 'FOO' ifPresent: [ :value | "Do something interesting." ]

Note: Before Pharo 7, #getEnv: message was used. This message is now deprecated and should not be used.