Hello world on the system console we use the following instruction in Obix:
system.console.write_line ( "Hello world" )
A single instruction can be executed through an operating system command that calls Obix. To do this, proceed as follows:
Open a system console (a text terminal in Linux, or a command line window in Windows)
Go to the root directory of your Obix installation
Enter one of the following commands, depending on whether you use a Unix-like operating system (e.g. Linux, Mac OS X or Unix) or a Windows system:
on Unix-like systems type
./obix.sh execute_instruction 'system.console.write_line ( "Hello world" )'
on Windows type
obix execute_instruction "system.console.write_line ( \"Hello world\" )"
After hitting the
Enter key the console displays:
On Unix-like systems your screen looks like this:
[albert@school obix]$ ./obix.sh execute_instruction 'system.console.write_line ( "Hello world" )' Hello world [albert@school obix]$
And this is how it looks like on Windows:
C:\tests\obix\>obix execute_instruction "system.console.write_line ( \"Hello world\" )" Hello world C:\tests\obix\>
In the above example we launch Obix and provide two command line arguments. The first argument,
execute_instruction, specifies the name of a command that Obix should execute. The second argument specifies the instruction we want to execute.
Instead of writing
From now on we will use the term 'Linux' instead of 'Unix-like system', because most Unix users use Linux. The following instructions given for Linux are also valid for any other Unix-like system.
Instead of writing
The above system command takes some time to execute, because the Obix environment has to be loaded, and the source code has to be translated into Java binary code. Later in this tutorial, you'll see how to write code that executes much faster.
Besides executing an instruction to display
Hello world, you can also ask Obix to evaluate an expression by using Obix's
evaluate command (or its short name
ev). In our case the expression is just a simple string literal. Hence, you can execute the following command in your operating system console:
on Linux: ./obix.sh evaluate '"Hello world"' on Windows: obix evaluate "\"Hello world\""
The result of evaluating an expression is always sent to the system's
out device. By default, the system's
out device is the system console. Therefore, executing the above instruction will display
Hello world on the system console
To store the result of your expression in file
tests/hello.txt, you can redirect the operating system's
out device. First, create a subdirectory
tests under Obix's root directory, to store any test files. Then use the operating system's redirection symbol
> to redirect Obix's output. Finally, display the file's content on the screen, as follows:
on Linux, type: mkdir tests ./obix.sh evaluate '"Hello world"' > tests/hello.txt cat tests/hello.txt on Windows, type: md tests obix evaluate "\"Hello world\"" > tests\hello.txt type tests\hello.txt
Just to give you a taste of what else you can do, here are some more examples (without explanations):
display the result of an arithmetic expression on the system console:
on Linux: ./obix.sh evaluate '(314+218)*17' on Windows: obix evaluate "(314+218)*17"
append the name of the currently logged in user to a text file:
on Linux: ./obix.sh ev 'system.user_name' >> tests/user_names.txt on Windows: obix ev "system.user_name" >> tests\user_names.txt
replace all uppercase letters in file
text_file.txt with lowercase letters and store the result in file
(first create file
text_file.txt in subdirectory
test and insert some text with lowercase and uppercase letters, then execute the following command)
on Linux: ./obix.sh ei 'system.out.write ( string = system.in.read_all.result.to_lower_case )' < tests/text_file.txt > tests/lowercase.txt on Windows: obix ei "system.out.write ( string = system.in.read_all.result.to_lower_case )" < tests\text_file.txt > tests\lowercase.txt
Executing a single instruction or evaluating a single expression is of course of very limited usage in practice. Therefore, the next level of executing code is to execute a set of instructions, as explained in the following chapter.