Pequod / PiROS manual ↑ Manual home
03 · Shell scripting

Scripting with Pequod

Beyond one-liners: reusable functions, libraries with include, and reading and writing files from the PiROS store — the toolkit for automation on the box.

Functions

Group logic into functions with their own local scope. Arguments pass by value; a missing argument is 0.

files/math.psh
function factorial($n) {
    if ($n <= 1) { return 1; }
    return $n * factorial($n - 1);   // recursion works
}
echo factorial(5);        // 120

Libraries with include

include "path"; runs another script in the current scope — its functions and constants become available. This is how you build a library and share it across scripts (and it's the main way to work around the 4096-byte file cap: split a big program into parts).

files/lib.psh
function sq($x)  { return $x * $x; }
function cube($x){ return $x * $x * $x; }
define("PI3", 3);        // integer 'pi' for demos
files/use.psh
include "files/lib.psh";
echo sq(6);             // 36
echo cube(3);           // 27
echo PI3;               // 3
require is accepted as a synonym for include.

Reading & writing files

Two built-ins move data in and out of the PiROS store. Paths follow the shell's rules — files/… is your data directory.

// read returns the file contents (or "" if missing)
$conf = read("files/settings.txt");
echo "config is " . strlen($conf) . " bytes\n";

// write returns 1 on success, 0 on failure
if (write("files/out.txt", "generated by psh\n")) {
    echo "saved\n";
} else {
    echo "write failed (over the 4096-byte cap?)\n";
}

Recipe: build a file from a template

Generate a config or page by assembling strings, then writing once.

files/gen.psh
$hosts = ["web1", "web2", "db1"];
$out = "# generated hosts\n";
$ip = 10;
foreach ($hosts as $h) {
    $out .= "192.168.1." . $ip . "  " . $h . "\n";
    $ip++;
}
write("files/hosts.txt", $out);
echo "wrote " . count($hosts) . " hosts\n";
bob@pros> psh files/gen.psh
wrote 3 hosts
bob@pros> cat files/hosts.txt
# generated hosts
192.168.1.10  web1
192.168.1.11  web2
192.168.1.12  db1

Recipe: a simple counter that persists

Read a number from a file, bump it, write it back. Useful for build numbers, visit counts, ids.

files/bump.psh
$n = int(read("files/counter.txt"));   // "" coerces to 0 the first time
$n++;
write("files/counter.txt", str($n));
echo "count is now $n\n";

Recipe: dispatch on an argument-style value

Pequod scripts don't take CLI flags directly, but you can drive behaviour from a small config value or a file — a clean pattern for reusable scripts.

$mode = read("files/mode.txt");
if ($mode == "full") {
    echo "running full pass\n";
} elseif ($mode == "quick") {
    echo "running quick pass\n";
} else {
    echo "unknown mode: " . $mode . "\n";
}
The 4096-byte cap. Scripts, and values read or written through psh, are bounded by the PiROS per-file cap of 4096 bytes. For larger logic, split into included libraries; for larger data, process it in chunks or across multiple files.
← Command reference Web pages with tintin →