ucoreSystem

System-level operations and I/O.

Strict Path Resolution: Unnarize enforces strict sandboxing. All file paths used in this library are resolved relative to the directory of the executing script, regardless of where the unnarize command is run. Absolute paths are typically rebased or rejected to prevent sandbox escape.

Functions

FunctionReturnsDescription
args() array Command line arguments
input(prompt) string Read user input from stdin
getenv(name) string Get environment variable value
fileExists(path) bool Check if file exists
writeFile(path, content) bool Write string content to file
readFile(path) string Read entire file as string
exec(command) string Execute shell command and return output
exit(code) void Exit the program with status code

args()

Returns an array of command line arguments. First element is the script path.

var args = ucoreSystem.args();
print("Script: " + args[0]);

if (length(args) > 1) {
    print("First arg: " + args[1]);
}

input(prompt)

Displays a prompt and reads a line from stdin.

var name = ucoreSystem.input("Enter your name: ");
print("Hello, " + name + "!");

var age = ucoreSystem.input("Enter your age: ");
print("You are " + age + " years old.");

getenv(name)

Returns the value of an environment variable, or empty string if not set.

var home = ucoreSystem.getenv("HOME");
var user = ucoreSystem.getenv("USER");
var path = ucoreSystem.getenv("PATH");

print("HOME: " + home);
print("USER: " + user);

fileExists(path)

Checks if a file exists at the given path.

if (ucoreSystem.fileExists("config.uon")) {
    print("Config found");
    ucoreUon.load("config.uon");
} else {
    print("Using defaults");
}

writeFile(path, content)

Writes string content to a file. Returns true on success, false on failure.

var content = "Hello, World!";
var success = ucoreSystem.writeFile("output.txt", content);

if (success) {
    print("File written successfully");
}

readFile(path)

Reads the entire file content as a string. Returns empty string if file cannot be read.

var content = ucoreSystem.readFile("data.txt");
if (length(content) > 0) {
    print("File content: " + content);
}

exec(command)

Executes a shell command and returns the output as a string.

// Get current directory listing
var files = ucoreSystem.exec("ls -la");
print(files);

// Get current date
var date = ucoreSystem.exec("date");
print("Today: " + date);

// Run Python script
var result = ucoreSystem.exec("python3 script.py");
print(result);

exit(code)

Terminates the program immediately with the specified exit code.

if (!ucoreSystem.fileExists("required.txt")) {
    print("Error: required.txt not found!");
    ucoreSystem.exit(1); // Exit with error code
}

// Continue if file exists...
print("All good!");
ucoreSystem.exit(0); // Exit with success

Example

./bin/unnarize examples/corelib/system/demo.unna arg1 arg2