Beyond one-liners: reusable functions, libraries with include, and reading and
writing files from the PiROS store — the toolkit for automation on the box.
Group logic into functions with their own local scope. Arguments pass by value; a missing
argument is 0.
function factorial($n) {
if ($n <= 1) { return 1; }
return $n * factorial($n - 1); // recursion works
}
echo factorial(5); // 120
includeinclude "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).
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.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";
}
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
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";
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";
}
included libraries; for larger data, process it in chunks or across multiple files.