Executing programs
Blocking and on the console
Reading to strings and lists
| // Equivalent of `listing=$( ps ax )`
val listing = "ps ax".exec<String>()
// More useful: get the output as lines, drop the header line.
val listing: List<String> = "ps ax".exec().drop(1)
|
Redirects
| "ps ax >listing.txt"()
"ps ax 2>listing.txt"()
// Multi-redirects.
"ps ax >listing1.txt >listing2.txt"()
// Redirect to a function; equivalent of 'ps ax | grep bash'
"ps ax >arg:1"({ if ("bash" in it) echo(it) })
|
Catching subprocess failures
| try {
"could-fail"()
} catch (e: Shell.SubprocessFailed) {
if (e.errorCode != 1)
throw e
echo("Child died with ${e.signalName}") // only on UNIX
}
|
A shorter way:
| val result: String? = runCatching { "could-fail".exec() }.getOrNull()
if (result != null) { etc }
|
Environment variables
| echo(env["FOO"])
env["HOME"] = mktmp().toString()
subshell {
env["SCOPED"] = "123" // Set var only for this sub-shell.
}
|
Run in the background
| val task = thread {
"npm install foo"()
}
task.join() // Wait for it.
|
Python
Python virtualenvs can be created in the hshell disk cache. The first time a set of packages is requested progress events are emitted
as the venv is initialized and packages installed. The PATH
and VIRTUAL_ENV
env vars are set in that shell.
When requested again the cache will be hit. The arguments can be a single path to a requirements file.
| virtualenv("pandas>=1.2.3", "mkdocs-material")
"mkdocs --help"()
|